简体   繁体   English

EF Core - 添加迁移时添加的 ID1 属性

[英]EF Core - ID1 property added when adding a migration

My scenario is for investment in properties (buildings).我的方案是投资物业(建筑物)。 An investor makes an investment in a property group, and that group can contain several individual properties.投资者对一个物业组进行投资,该组可以包含多个单独的物业。 As investors can invest in multiple groups, and any group can have multiple investors, the investment itself is a many-to-many relationship between the investors and property groups.由于投资人可以投资于多个群体,而任何一个群体都可以有多个投资人,投资本身就是投资人与地产群体之间的多对多关系。

Ignoring the simple properties for clarity, the models look like this...为了清楚起见,忽略简单的属性,模型看起来像这样……

public class Investor {
  public int ID { get; set; }

  public ObservableCollection<Investment> Investments { get; set; }
}

public class Investment {
  public int ID { get; set; }

  public int InvestorID { get; set; }
  public virtual Investor Investor { get; set; }

  public int PropertyGroupID { get; set; }
  public virtual PropertyGroup PropertyGroup { get; set; }
}

public class PropertyGroup {
  public int ID { get; set; }

  public ObservableCollection<Property> Properties { get; set; }
  public ObservableCollection<Investment> Investments { get; set; }
}

public class Property {
  public int ID { get; set; }

  public int PropertyGroupID { get; set; }
  public virtual PropertyGroup PropertyGroup { get; set; }
}

This all works fine.这一切都很好。

I now have the requirement to generate and store documents.我现在有生成和存储文件的需求。 These will always be associated with a specific investor, but may also be associated with a property or a group.这些将始终与特定投资者相关联,但也可能与财产或团体相关联。

I added the following class...我添加了以下 class...

public class Document {
  public int ID { get; set; }
  // Other simple properties omitted for clarity

  public int InvestorId { get; set; }
  public Investor Investor { get; set; } = null!;

  public int? PropertyGroupId { get; set; }
  public PropertyGroup? PropertyGroup { get; set; }
  
  public int? PropertyId { get; set; }
  public Property? Property { get; set; }
}

In order to provide the navigation links into the documents, I added the following line into the Investor , PropertyGroup and Property models...为了提供文档的导航链接,我在InvestorPropertyGroupProperty模型中添加了以下行...

public ObservableCollection<Investment> Documents { get; set; }

When I try to add a migration, I get an error " Unable to determine the relationship represented by navigation 'Investment.Investor' of type 'Investor'. "当我尝试添加迁移时,出现错误“无法确定类型为‘Investor’的导航‘Investment.Investor’所代表的关系。

Reading the output from the migration, I see a message " No relationship from 'Investment' to 'Investor' has been configured by convention because there are multiple properties on one entity type - {'Investor'} that could be matched with the properties on the other entity type - {'Documents', 'Investments'}. "从迁移中读取 output,我看到一条消息“没有按照约定配置从‘Investment’到‘Investor’的关系,因为一个实体类型上有多个属性 - {'Investor'} 可以与上的属性匹配另一种实体类型 - {'Documents', 'Investments'}。

I don't understand either message, as I do have an explicit relationship defined from Investment to Investor as you can see above.我不理解任何一条消息,因为我确实有从InvestmentInvestor定义的明确关系,正如您在上面看到的那样。 I have an int property called InvestorId and a navigation property of type (and name) Investor .我有一个名为InvestorIdint属性和一个类型(和名称) Investor的导航属性。 I'm not sure what the last bit of the second message means.我不确定第二条消息的最后一点是什么意思。

However, I added the following to OnModelCreating ...但是,我将以下内容添加到OnModelCreating ...

builder.Entity<Investment>()
  .HasOne<Investor>()
  .WithMany(i => i.Investments)
  .HasForeignKey(i => i.InvestorID)
  .OnDelete(DeleteBehavior.Restrict);

This got rid of that error, but gave a similar one, " Unable to determine the relationship represented by navigation 'Investment.PropertyGroup' of type 'PropertyGroup'. " I added the following...这摆脱了那个错误,但给出了一个类似的错误,“无法确定类型为‘PropertyGroup’的导航‘Investment.PropertyGroup’所代表的关系。 ”我添加了以下内容……

builder.Entity<Investment>()
  .HasOne<PropertyGroup>()
  .WithMany(i => i.Investments)
  .HasForeignKey(i => i.PropertyGroupID)
  .OnDelete(DeleteBehavior.Restrict);

This allowed the migration to be added, but it included the following in the Up method...这允许添加迁移,但它在Up方法中包含以下内容......

migrationBuilder.AddColumn<int>(
  name: "InvestorID1",
  table: "Investments",
  type: "int",
  nullable: false,
  defaultValue: 0);

migrationBuilder.AddColumn<int>(
  name: "PropertyGroupID1",
  table: "Investments",
  type: "int",
  nullable: false,
  defaultValue: 0);

I can see the migration output contains a warning " The foreign key property 'Investment.InvestorID1' was created in shadow state because a conflicting property with the simple name 'InvestorID' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type. " but I don't understand what it means.我可以看到迁移 output 包含警告“外键属性‘Investment.InvestorID1’是在影子 state 中创建的,因为实体类型中存在简单名称‘InvestorID’的冲突属性,但要么未映射,要么已被使用对于另一种关系,或者与关联的主键类型不兼容。 ”但我不明白这是什么意思。

Anyone able to explain what I'm doing wrong?谁能解释我做错了什么? Thanks谢谢

There is something missed(typo), to make relation with Document you have to define collection props of Document not Investments so change遗漏了一些东西(打字错误),要与Document建立关系,您必须定义Document而不是Investments的集合props ,因此请更改

public ObservableCollection<Investment> Documents { get; set; }

to

public ObservableCollection<Document> Documents { get; set; }

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

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