Len*_*lob 10 .net c# asp.net asp.net-mvc
当我迁移新模型和数据时,多个外键出现以下错误:
外键属性“InsurancePolicy.InsuranceSubjectID1”是在影子状态下创建的,因为实体类型中存在简单名称“InsuranceSubjectID”的冲突属性,但未映射、已用于其他关系或与关联的属性不兼容主键类型。
奇怪的是,我在所有模型中定义的关系都相同,但有些可以正常工作(存储不带 1 的 FK),有些则不行。
我的模型示例:
public class InsurancePolicy
{
    public int InsurancePolicyID { get; set; }
    [DataType(DataType.Currency)]
    [Column(TypeName = "money")]
    public decimal FinalSum { get; set; }
    public DateTime DateFrom { get; set; }
    public DateTime DateTo { get; set; }
    public int? InsuredID { get; set; }
    public Insured? Insured { get; set; }
    public int? InsuranceSubjectID;
    public InsuranceSubject? InsuranceSubject { get; set; }
    public int? InsuranceSubtypeID;
    public InsuranceSubtype? InsuranceSubtype { get; set; }
}
public class InsuranceSubject
{
    public int InsuranceSubjectID { get; set; }
    [Required]
    [StringLength(50)]
    public string Title { get; set; }
    public string Description { get; set; }
    [DataType(DataType.Currency)]
    [Column(TypeName = "money")]
    public decimal EstimatedValue { get; set; }
    public int? InsuredID;
    public Insured? Insured;
    public int? InsuranceSubjectTypeID { get; set; }
    public InsuranceSubjectType? InsuranceSubjectType { get; set; }
    public ICollection<InsurancePolicy>? InsurancePolicies { get; set; }
}
我尝试了没有外键属性的代码,只留下了引用导航属性(删除了 int 并留下了对象),它工作得很好,但我需要 FK int 以便将来实现。
以下是 GitHub 存储库链接,可获取更多见解(分支模型扩展):https://github.com/lenartgolob/Insurance-IS/tree/model-extension
gra*_*der 13
对我来说还可以。
我有父母/孩子的关系。
我放入了标量(在子项上),其中还包括父项 FK(作为 int),并为(相同的)标量 FK“int”定义了 orm 映射(再次在子项上)。
public class MyParent
{
    public int MyParentKey { get; set; }
    public ICollection<MyChild> MyKids { get; set; }
}
public class MyChild
{
    public int MyChildKey { get; set; }
    public int MyParentKey { get; set; } /* FK Scalar */
    public MyParent TheMyParent { get; set; }
}
我在 MyChildOrmMap 上对 MyParentKey(在本例中为“int”)进行了 Orm 映射。
但我没有通过指向该对象的属性对对象(在本例中为“MyParent”对象)进行orm映射。又名“TheMyParent”属性。
一旦我为对象(在本例中为“TheMyParent”)(以及倒数的 MyKids)编写了 1:N 关系,错误就消失了。
又名,我错过了下面的“HasOne/WithMany”orm 映射(下面的代码将在 MyChildMap (流畅的 orm 映射)中)
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
        builder.HasOne<MyParent>(me => me.TheMyParent)
            .WithMany(parent => parent.MyKids)
            .HasForeignKey(me => me.MyParentKey)
            .HasConstraintName("FK_MyChild_To_MyParent_MyParentKey");  
我public virtual在参考导航属性前面添加了它,它解决了这个问题。
| 归档时间: | 
 | 
| 查看次数: | 17005 次 | 
| 最近记录: |