简体   繁体   English

实体框架中的外键问题一对多

[英]Problem with foreign key in Entity Framework one to many

I am having some foreign key problems, it turns out that I am doing a revision to an old system in which a new table is added, and in doing so, the following arises.我遇到了一些外键问题,事实证明我正在对添加新表的旧系统进行修订,这样做时会出现以下情况。

In short, I have the following 3 classes.简而言之,我有以下 3 个课程。

public class StepUp
{
    [Key]
    [Column(Order = 0)]    
    public string CodigoDelfos { get; set; }    
    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Revision { get; set; }
    public virtual ICollection<Accesorio_x_Trafos> Accesorios { get; set; }
    //More irrelevant properties..
}

public class Transformador 
{
    [Key]
    [Column(Order = 0)]
    public string Codigo_delfos { get; set; }
    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Revision { get; set; }
    public virtual ICollection<Accesorio_x_Trafos> Accesorio_x_Trafos { get; set; }
    //More irrelevant properties..
}

public class Accesorio_x_Trafos
{
    [Key]
    [Column(Order = 0)]
    public string CodigoAccesorio { get; set; }        
    [Key]
    [Column(Order = 1)]
    public string CodigoDelfos { get; set; }
    [Key]
    [Column(Order = 2)]
    public int Revision { get; set; }       
    public int? Cantidad { get; set; }    
}

The issue is that both StepUp and Transformador have a 1- * relationship with Accessorio_x_Trafos .问题是StepUpTransformador都与Accessorio_x_Trafos有 1- * 关系。

When migrating, the database is created correctly (FKs are added)迁移时,正确创建数据库(添加FK)

在此处输入图片说明

(Ignore the FK Accesorios which is one of the properties that I removed so as not to have so much code) (忽略 FK Accesorios这是我删除的属性之一,以免有太多代码)

The issue is that when I try, both modify an object of type Transformer (I do it as follows)问题是,当我尝试时,两者都修改了 Transformer 类型的对象(我按如下方式进行)

//Get any object..
var transformador = mContext.Transformador.First();
//Add random data..
transformador.Accesorio_x_Trafos = new List<Accesorio_x_Trafos>
{
    new Accesorio_x_Trafos 
        { CodigoDelfos = transformador.Codigo_delfos, Revision = transformador.Revision, Cantidad = 1, CodigoAccesorio = "05-CBPA0150000802000P0800"},
    new Accesorio_x_Trafos
        { CodigoDelfos = transformador.Codigo_delfos, Revision = transformador.Revision, Cantidad = 2, CodigoAccesorio = "05-CBPA0150000803150P0700"},
    new Accesorio_x_Trafos
        { CodigoDelfos = transformador.Codigo_delfos, Revision = transformador.Revision, Cantidad = 3, CodigoAccesorio = "05-CBPA0150000803150P0700"}
};
//Save it
mContext.SaveChanges();

But this throws the follow exception但这会引发以下异常

An error occurred while saving entities that do not expose foreign key properties for their relationships.保存未公开其关系的外键属性的实体时发生错误。 The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. EntityEntries 属性将返回 null,因为无法将单个实体标识为异常源。 Handling of exceptions while...处理异常时...

And in the InnerException the而在 InnerException 中

Instrucción INSERT en conflicto con la restricción FOREIGN KEY \\"FK_dbo.Accesorio_x_Trafos_dbo.StepUps_CodigoDelfos_Revision\\". Instrucción INSERT en conflicto con la restrictción FOREIGN KEY \\"FK_dbo.Accesorio_x_Trafos_dbo.StepUps_CodigoDelfos_Revision\\"。 El conflicto ha aparecido en la base de datos \\"DBTrafos\\", tabla \\"dbo.StepUps\\".\\r\\nSe terminó la instrucción. El conflicto ha aparecido en la base de datos \\"DBTrafos\\", tabla \\"dbo.StepUps\\"。\\r\\nSe terminó la instrucción。

How can i deal with this?我该如何处理?

I solve my issue by creating an abstract class that replaced Accessory_x_Trafos in such a way, that you then specify each inherited class with its foreign key and corresponding navigation property.我通过创建一个抽象类来解决我的问题,该类以这种方式替换了Accessory_x_Trafos ,然后您可以使用其外键和相应的导航属性指定每个继承的类。

public abstract class AccesorioTransformador
{
    public Guid Id { get; set; }
    public string CodigoAccesorio { get; set; }
    public int Cantidad { get; set; }   
}

public class AccesorioStepUp : AccesorioTransformador
{
    public string CodigoDelfos { get; set; }
    public int Revision { get; set; }   
    [ForeignKey(nameof(CodigoDelfos)+","+nameof(Revision))]   
    public StepUp.StepUp Transformador { get; set; }
}

public class AccesorioDistribucion : AccesorioTransformador
{
    public string CodigoDelfos { get; set; }
    public int Revision { get; set; }           
    [ForeignKey(nameof(CodigoDelfos) + "," + nameof(Revision))]
    public Transformador Transformador { get; set; }
}

In this way, in each of the corresponding classes, I used properties, either type ICollection<AccesorioStepUp> or type ICollection<AccesorioDistribucion> .这样,在每个相应的类中,我使用了属性,类型为ICollection<AccesorioStepUp>或类型为ICollection<AccesorioDistribucion>

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

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