something is missing associate between Two DataTable Classes

Thread is closed for posting
4 posts, 2 answers
  1. mustafa salih
    mustafa salih avatar
    2 posts
    Member since:
    Feb 2017

    Posted 04 Apr 2017 Link to this post

    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;
           }
  2. Answer
    Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    324 posts

    Posted 05 Apr 2017 Link to this post

    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.
  3. mustafa salih
    mustafa salih avatar
    2 posts
    Member since:
    Feb 2017

    Posted 05 Apr 2017 in reply to Viktor Zhivkov Link to this post

    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)

  4. Answer
    Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    324 posts

    Posted 11 Apr 2017 Link to this post

    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.
Back to Top