简体   繁体   English

实体框架代码的前1对1..0关系

[英]Entity Framework Code First 1 to 1..0 relationship

I seem to have a unique problem with Entity Framework achieving 1 to 0..1 relationship; 我似乎对实体框架实现1到0..1的关系有一个独特的问题; I have two entities. 我有两个实体。

  1. FamilyMember 家庭成员
  2. ImageStore 图片库

FamilyMember stores the person's details and I have ImageStore where profile picture is being stored but it is not required. FamilyMember存储该人的详细信息,而我有ImageStore,用于存储个人资料图片,但这不是必需的。 So FamilyMember may or may not have a profile picture. 因此,FamilyMember可能有也可能没有个人资料图片。 The problem I have is: When I have a profile picture stored in ImageStore, it retrieves the FamilyMember object with ImageStore data but when I don't have profile picture in ImageStore, whole FamilyMember object comes out to be null instead of only ImageStore object in FamilyMember being null. 我遇到的问题是:当我在ImageStore中存储了个人资料图片时,它会检索包含ImageStore数据的FamilyMember对象,但是当我在ImageStore中没有个人资料图片时,整个FamilyMember对象将变为null,而不仅仅是ImageStore对象。 FamilyMember为空。 I struggled on this issue for a day before returning for a help. 我在这个问题上奋斗了一天,然后才寻求帮助。

I am using Sqlite with Entity Framework 6.0 我在实体框架6.0中使用Sqlite

Let me start with two models: 让我从两个模型开始:

FamilyMember 家庭成员

public class FamilyMember : PropertyChangedNotification
    {
        [Key]
        public long FamilyMemberId { get; set; }
        [ForeignKey("ImageStore")]
        public int? ProfilePictureId { get; set; }       
        public ImageStore ImageStore { get; set; }
    }

ImageStore 图片库

public class ImageStore : PropertyChangedNotification
    {
        [Key]
        public int ImageStoreId { get; set; }
        [Required]
        public string ImageFile { get; set; }
        [Required]
        public Byte[] ImageBlob { get; set; }
        public string FileName { get; set; }
        [Required]
        public int FileSize { get; set; }        
    }

Here is my DataContext 这是我的DataContext

public class ADataContext : DbContext
    {
        public ADataContext()
            : base("name=ADataContext")
        {

        }

        public ADataContext(string filename, string password)
            : base(new SQLiteConnection()
            {
                ConnectionString =
                    new SQLiteConnectionStringBuilder()
                    {DataSource = filename, ForeignKeys = true, Password = password }
                        .ConnectionString
            }, true)
        {
        }

        public ADataContext(string filename)
            : base(new SQLiteConnection()
            {
                ConnectionString =
                    new SQLiteConnectionStringBuilder() { DataSource = filename, ForeignKeys = true }
                        .ConnectionString
            }, true)
        {
            Database.SetInitializer<ADataContext>(null);
        }

        public DbSet<FamilyMember> FamilyMembers { get; set; }        
        public DbSet<ImageStore> ImageStore { get; set; }        

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {                
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();    
            modelBuilder.Entity<FamilyMember>().HasOptional(s => s.ImageStore);                
        }
    }

And here is the LINQ statement where I am getting the FamilyMember Data using eager loading 这是LINQ语句,我在其中使用急切加载获取FamilyMember数据

familyMember =  _dbContext.FamilyMembers.Include("ImageStore").FirstOrDefault(x => x.FamilyMemberId == memberId);

I think, I finally found a solution to a problem. 我想,我终于找到了解决问题的办法。 Database table and model was not matching. 数据库表和模型不匹配。 There was one required field in model which was not created with NOT NULL constraint. 模型中有一个必填字段,不是使用NOT NULL约束创建的。 When I created a row in database, that field was null and when entity framework was getting that row, it was silently dropping it instead of complaining about it. 当我在数据库中创建一行时,该字段为null,而当实体框架获取该行时,它会默默地删除它而不是抱怨它。 Finally fixed the problem by bringing the database in sync with the model. 最后,通过使数据库与模型同步来解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM