简体   繁体   English

EntityFramework 1至0、1或许多关系

[英]EntityFramework 1 to 0, 1 or many relationship

I am struggling to create a relationship in Entity framework between three tables.. I am developing WPF application using C#, Sqlite 3.0 and Entity Framework 6. 我正在努力在实体表中的三个表之间创建一个关系。我正在使用C#,Sqlite 3.0和实体框架6开发WPF应用程序。

I have Following tables(classes): 我有以下表(类):

  1. Investor 投资者
  2. Investment 投资
  3. ImageStore ImageStore

I want to create a model so that Investor and Investment (and future other classes) can store either 0, 1 or Many images.. (as it is obvious from class names that Investor can have only one profile image and Investment class can have multiple images) My ImageStore class looks something like this: 我想创建一个模型,以便“投资者和投资”(以及将来的其他类别)可以存储0、1或“很多”图像。(从类名可以明显看出,投资者只能有一个配置文件图像,而投资类可以有多个图片)我的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; }


        //public virtual ImageData ImageData { get; set; }
    }

In order to create 1 to 0,1 or Many relationship, I created one more intermediate table called: ImageData as seen below (I don't know whether it is really a good approach but that is only what I can think of right now..) 为了创建1到0,1或Many关系,我创建了另一个中间表:ImageData,如下所示(我不知道这是否真的是一种好方法,但这只是我现在能想到的。 。)

public class ImageData : PropertyChangedNotification
    {
        [Key]
        public int ImageDataId { get; set; }

        [ForeignKey("Investment")]
        public long? InvestmentId { get; set; }

        [ForeignKey("Investor")]
        public long? InvestorId { get; set; }

        [ForeignKey("ImageStore")]
        public int ImageStoreId { get; set; }

        public virtual ImageStore ImageStore { get; set; }

        public virtual Investment Investment { get; set; }

       public virtual Investor Investor { get; set; }
    }

My Investor class looks like this: 我的投资者类如下:

public class Investor : PropertyChangedNotification
    {
        [Key]
        public long InvestorId { get; set; }        

        [NotMapped]
        [ForeignKey("ImageData")]
        public List<int> ImageDataList { get; set; }

        public virtual ICollection<ImageData> ImageDataCollection { get; set; }
        public virtual ICollection<Investment> Investments { get; set; }        
    }

My Investment Class Looks like this: 我的投资类别如下:

 public class Investment : PropertyChangedNotification
    {
        [Key]
        public long InvestmentId { get; set; }

       [ForeignKey("Investor")]
       [Required]
       public long FirstInvestorId { get; set; }       


        [NotMapped]
        [ForeignKey("ImageData")]
        public List<int> ImageDataList { get; set; }

        public virtual ICollection<ImageData> ImageDataCollection { get; set; }       

        public virtual Investor Investor { get; set; }

        [NotMapped]
        [Required (ErrorMessage = "First Investor is Required")]
        public Investor FirstInvestor { get; set; }
    }

This is my related fluent configuration: 这是我相关的流利配置:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // MyData Database does not pluralize table names
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            modelBuilder.Entity<Investor>().HasOptional(s => s.ImageDataCollection);
            modelBuilder.Entity<Investment>().HasOptional(s => s.ImageDataCollection);
            //modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
        }

When I start debugging the application, I get the following error: 开始调试应用程序时,出现以下错误:

  1. ImageData_Investment_Source: : Multiplicity is not valid in Role 'ImageData_Investment_Source' in relationship 'ImageData_Investment'. ImageData_Investment_Source::多重性在关系“ ImageData_Investment”中的角色“ ImageData_Investment_Source”中无效。 Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*' 由于从属角色属性不是关键属性,因此从属角色多重性的上限必须为“ *”
  2. ImageData_Investor_Source: : Multiplicity is not valid in Role 'ImageData_Investor_Source' in relationship 'ImageData_Investor'. ImageData_Investor_Source::多重性在关系“ ImageData_Investor”中的角色“ ImageData_Investor_Source”中无效。 Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*' 由于从属角色属性不是关键属性,因此从属角色多重性的上限必须为“ *”

Can someone please suggest me either a solution to this problem and/or optimal approach to achieve what I need, I really will appreciate. 有人可以建议我解决该问题和/或实现我所需要的最佳方法,我将不胜感激。 Thanks 谢谢

The fluent configuration 流利的配置

modelBuilder.Entity<Investor>().HasOptional(s => s.ImageDataCollection);
modelBuilder.Entity<Investment>().HasOptional(s => s.ImageDataCollection);

is incomplete. 不完整。

Since the you already have the necessary data annotations and navigation / FK properties, you can simply remove it. 由于您已经具有必要的数据注释和导航/ FK属性,因此只需将其删除即可。 Or if you want to provide fluent configuration (which I personally prefer because it allows you to specify everything explicitly and not rely on conventions and specifically for relationships, not so intuitive ForegnKey and InverseProperty data annotations), then you should make sure it reflects exactly the presense/absence of the navigation and FK properties in the involved entities. 或者,如果您想提供流畅的配置(我个人更喜欢,因为它允许您显式指定所有内容,而不依赖约定,特别是针对关系,而不是直观的ForegnKeyInverseProperty数据注释),那么您应确保它准确地反映了所涉及实体中是否存在导航和FK属性。

The correct fluent configuration reflecting your model so far is like this: 到目前为止,反映您的模型的正确的流利配置是这样的:

modelBuilder.Entity<Investor>()
    .HasMany(e => e.ImageDataCollection)
    .WithOptional(e => e.Investor)
    .HasForeignKey(e => e.InvestorId);

modelBuilder.Entity<Investment>()
    .HasMany(e => e.ImageDataCollection)
    .WithOptional(e => e.Investment)
    .HasForeignKey(e => e.InvestmentId);

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

相关问题 EntityFramework 6关系映射到许多项目 - EntityFramework 6 relationship mapping to many items 在EntityFramework中配置多对多关系 - Configuring many-to-many relationship in EntityFramework EntityFramework同桌多对多关系 - EntityFramework Same Table Many to Many Relationship 0..1与带有EntityFramework Core的Fluent API的许多关系 - 0..1 to many relationship with Fluent API with EntityFramework Core 正确编辑多对多关系EntityFramework C# - Edit many to many relationship EntityFramework C# correctly EntityFramework CodeFirst:CASCADE DELETE用于同一个表的多对多关系 - EntityFramework CodeFirst: CASCADE DELETE for same table many-to-many relationship 在EntityFramework中,如何重新加载多对多关系中的实体? - In EntityFramework how do you reload entities in a Many to Many relationship? EntityFramework:将现有的一对多关系映射到模型 - EntityFramework : Mapping existing one-to-many relationship to Model 如何在 EntityFramework 中配置这种关系(1 -&gt; 许多,加 1 -&gt; 0 或 1,无连接表) - How can I configure this relationship in EntityFramework (1 -> many, plus 1 -> 0 or 1, no join table) 如何在实体框架中配置一对多关系以及继承 - How to configure one to many relationship along with inheritance in entityframework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM