Updating primary key in Entity Framework Code First

白昼怎懂夜的黑 提交于 2020-01-05 05:00:12

问题


I want to update a mediate table's primary keys. This post suggests to take another primary key and don't change it, but we're using our tables in other non-EF projects, I didn't designed them and I'm not able to change them. Do I have any other option? Anything? Even deleting the old record and inserting a new one. I just don't know how to retrieve the old values.

This is my class:

public class ZChangeUnits : User
{
    [Key]
    [Column(TypeName = "VARCHAR", Order = 0), StringLength(4)]
    public string CCode1 { get; set; }

    [ForeignKey("CCode1")]
    public virtual ZUnits ZUnits1 { get; set; }

    [Key]
    [Column(TypeName = "VARCHAR", Order = 1), StringLength(4)]
    public string CCode2 { get; set; }

    [ForeignKey("CCode2")]
    public virtual ZUnits ZUnits2 { get; set; }

    [Column(TypeName = "NUMERIC")]
    [DecimalPrecision(18, 5)]
    public decimal NZarib { get; set; }
}

UPDATE

I've posted the schema below, the left table is used for unit conversion. This is actually the suggested method in this answer.

I can't think of any other way to implement that. Whether I should update my table's design or some EF code will do the job. Any help and solution is welcome. :)

来源:https://stackoverflow.com/questions/29891544/updating-primary-key-in-entity-framework-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!