![](/img/trans.png)
[英]Ef core Creating additional column for a navigation property with Name *ID1
[英]EF Core - ID1 property added when adding a migration
我的方案是投资物业(建筑物)。 投资者对一个物业组进行投资,该组可以包含多个单独的物业。 由于投资人可以投资于多个群体,而任何一个群体都可以有多个投资人,投资本身就是投资人与地产群体之间的多对多关系。
为了清楚起见,忽略简单的属性,模型看起来像这样……
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; }
}
这一切都很好。
我现在有生成和存储文件的需求。 这些将始终与特定投资者相关联,但也可能与财产或团体相关联。
我添加了以下 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; }
}
为了提供文档的导航链接,我在Investor
、 PropertyGroup
和Property
模型中添加了以下行...
public ObservableCollection<Investment> Documents { get; set; }
当我尝试添加迁移时,出现错误“无法确定类型为‘Investor’的导航‘Investment.Investor’所代表的关系。 ”
从迁移中读取 output,我看到一条消息“没有按照约定配置从‘Investment’到‘Investor’的关系,因为一个实体类型上有多个属性 - {'Investor'} 可以与上的属性匹配另一种实体类型 - {'Documents', 'Investments'}。 ”
我不理解任何一条消息,因为我确实有从Investment
到Investor
定义的明确关系,正如您在上面看到的那样。 我有一个名为InvestorId
的int
属性和一个类型(和名称) Investor
的导航属性。 我不确定第二条消息的最后一点是什么意思。
但是,我将以下内容添加到OnModelCreating
...
builder.Entity<Investment>()
.HasOne<Investor>()
.WithMany(i => i.Investments)
.HasForeignKey(i => i.InvestorID)
.OnDelete(DeleteBehavior.Restrict);
这摆脱了那个错误,但给出了一个类似的错误,“无法确定类型为‘PropertyGroup’的导航‘Investment.PropertyGroup’所代表的关系。 ”我添加了以下内容……
builder.Entity<Investment>()
.HasOne<PropertyGroup>()
.WithMany(i => i.Investments)
.HasForeignKey(i => i.PropertyGroupID)
.OnDelete(DeleteBehavior.Restrict);
这允许添加迁移,但它在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);
我可以看到迁移 output 包含警告“外键属性‘Investment.InvestorID1’是在影子 state 中创建的,因为实体类型中存在简单名称‘InvestorID’的冲突属性,但要么未映射,要么已被使用对于另一种关系,或者与关联的主键类型不兼容。 ”但我不明白这是什么意思。
谁能解释我做错了什么? 谢谢
遗漏了一些东西(打字错误),要与Document
建立关系,您必须定义Document
而不是Investments
的集合props
,因此请更改
public ObservableCollection<Investment> Documents { get; set; }
到
public ObservableCollection<Document> Documents { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.