This is a migrated thread and some comments may be shown as answers.

something is missing associate between Two DataTable Classes

3 Answers 22 Views
Databases and Data Types
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
mustafa salih
Top achievements
Rank 1
mustafa salih asked on 04 Apr 2017, 03:37 PM

i try to associate one to one and one to many relationship with tables and it corrects on MysqlDb, but when i call data with UserRepository which derives from OpenAccessBaseRepository<User>, i got an null property which types UserCity, in fact, UserCityId is not null in same instance, also same with other relationship types. (i read the documentation, i tried different method (e.i. IsManaged()) but i couldn't find the solution)

For Example; User.UserCity = new UserCity() is not work, also, User.UserLogins.Add doesnt work because there are null refference,

public class User
   {
       public User()
       {
           UserLogins = new List<UserLogin>();
       }
 
       public int UserId { get; set; }
       public string Name { get; set; }
 
       public int UserCityId { get; set; }
       public virtual UserCity UserCity { get; set; }
       public virtual IList<UserLogin> UserLogins { get; set; }
   }
 
   public class UserLogin
   {
       public Guid UserSession { get; set; }
       public string Provider { get; set; }
       public int UserId { get; set; }
       public virtual User User { get; set; }
   }
 
   public class UserCity
   {
       public int UserCityId { get; set; }
       public string Name { get; set; }
   }
 
 
 
   ///// FluentModelMetadataSource.cs
    protected override IList<MappingConfiguration> PrepareMapping()
       {
           var conf = new List<MappingConfiguration>();
           var userMapping = new MappingConfiguration<User>();
           userMapping.MapType(m => new
           {
               UserId = m.UserId,
               Name = m.Name,
               UserCity = m.UserCity,
               UserCityId = m.UserCityId,
               UserLogins = m.UserLogins
           }).ToTable("users");
 
           userMapping.HasProperty(p => p.UserId).IsIdentity(KeyGenerator.Autoinc);
           userMapping.HasAssociation(p => p.UserCity)
               .HasConstraint((p, c) => p.UserCityId == c.UserCityId)
               .ToColumn("UserCityId")
               .IsRequired();
           conf.Add(userMapping);
 
           var usercityMapping = new MappingConfiguration<UserCity>();
           usercityMapping.MapType(m => new
           {
               UserCityId = m.UserCityId,
               Name = m.Name
           }).ToTable("cities");
           usercityMapping.HasProperty(p => p.UserCityId).IsIdentity(KeyGenerator.Autoinc);
           conf.Add(usercityMapping);
 
           var userloginMapping = new MappingConfiguration<UserLogin>();
           userloginMapping.MapType(m => new
           {
               UserSession = m.UserSession,
               Provider = m.Provider,
               User = m.User,
               UserId = m.UserId
           }).ToTable("userlogins");
           userloginMapping.HasProperty(p => p.UserSession).IsIdentity(KeyGenerator.Guid);
           userloginMapping.HasAssociation(p => p.User)
               .WithOpposite(c => c.UserLogins)
               .HasConstraint((p, c) => p.UserId == c.UserId)
               .ToColumn("UserId")
               .IsRequired();
           conf.Add(userloginMapping);
           return conf;
       }
 
 
       //// SAMPLE CALLS
        public UserRepository Users { get; set; }
        public UserCityRepository UserCities { get; set; }
        public UserLoginRepository UserLogins { get; set; }
         public List<User> Profile()
       {
           foreach (var data in UserLogins.GetAll()) UserLogins.Delete(data);
           foreach (var data in Users.GetAll()) Users.Delete(data);
           foreach (var data in UserCities.GetAll()) UserCities.Delete(data);
 
           UserCity usrc = new UserCity { Name = "Istanbul" };
           UserCities.AddNew(usrc);//adding correctly
           User usr = new User
           {
               UserCity = usrc,
               Name = "mustafa"
           };
           Users.AddNew(usr);//adding correctly
           var users = Users.GetAll().ToList();//users are getting but their 'UserCity' properties are null problem is here, 'UserCityId' is not nul
 
           UserLogin usrl = new UserLogin
           {
               UserSession = Guid.NewGuid(),
               Provider = "google",
               UserId = users.FirstOrDefault().UserId
           };
           UserLogins.AddNew(usrl);//adding correctly
            
              users = Users.GetAll().ToList();//Users.FirstOrDefault().UserLogins is NULL, so problem is here
           return users;
       }

3 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Zhivkov
Telerik team
answered on 05 Apr 2017, 11:53 AM
Hi mustafa salih,

If I understand you correctly you are having trouble with reading related data and not saving it to the database.
If this is the case you can try using Fetch Strategies on your LINQ queries:
1.using Telerik.OpenAccess;
2. 
3.// assuming Users.GetAll() returns IQueryable<T> and you have added the using above
4.var query = Users.GetAll().Include(u => u.UserCity);
5.var result = query.ToList();


Regards,
Viktor Zhivkov
Telerik by Progress
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
0
mustafa salih
Top achievements
Rank 1
answered on 06 Apr 2017, 12:04 AM

definitely it is, works for get all properties,

is there a way to insert like 'User.UserLogins.Add()' this ?.

also 

second question is. can i use InSingletonScope for FlueModel on MYSQLDB, can you give any information for this, or standart implementation is enough for this? (since concurrency connection is problem on mysql)

0
Accepted
Viktor Zhivkov
Telerik team
answered on 11 Apr 2017, 08:49 AM
Hello,

Telerik Data Access uses persistence by reachability, meaning that if you build a graph (or a tree) of interconnected persistent objects and add one of the graph members, the whole graph will get persisted.

In your scenario for adding a UserLogin to an User - the .Add method will add (as persistent) the new login if the User instance is already added (as we call it - managed) by an OpenAccessContext instance.

I am not aware of difficulties with managing MySql connections with our runtime, you are welcome to experiment with the configuration if you are experiencing any issues.

Regards,
Viktor Zhivkov
Telerik by Progress
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Tags
Databases and Data Types
Asked by
mustafa salih
Top achievements
Rank 1
Answers by
Viktor Zhivkov
Telerik team
mustafa salih
Top achievements
Rank 1
Share this question
or