简体   繁体   English

实体框架中的主键值

[英]Primary key value in Entity Framework

Entity layout contains int value of venue ( VenueId prop), its own id and other information.实体布局包含场地的int值( VenueId prop)、自身的id等信息。

CONSTRAINT [FK_Venue_Layout] FOREIGN KEY ([VenueId]) REFERENCES [dbo].[Venue] ([Id])

When I trying to add two layouts with the same VenueId , I'm getting this error当我尝试添加具有相同VenueId两个布局时,出现此错误

The changes to the database were committed successfully, but an error occurred while updating the object context.对数据库的更改已成功提交,但在更新对象上下文时发生错误。 The ObjectContext might be in an inconsistent state. ObjectContext 可能处于不一致的状态。 Inner exception message: Saving or accepting changes failed because more than one entity of type 'DataAccess.Models.LayoutModel' have the same primary key value.内部异常消息:保存或接受更改失败,因为多个“DataAccess.Models.LayoutModel”类型的实体具有相同的主键值。 Ensure that explicitly set primary key values are unique.确保显式设置的主键值是唯一的。 Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model.确保在数据库和实体框架模型中正确配置了数据库生成的主键。 Use the Entity Designer for Database First/Model First configuration.将实体设计器用于数据库优先/模型优先配置。 Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration."使用“HasDatabaseGeneratedOption”流畅 API 或“DatabaseGeneratedAttribute”进行代码优先配置。”

My entity code:我的实体代码:

[Table("Layout")]
public class LayoutModel
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int Id { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int VenueId { get; set; }

    public string Description { get; set; }
}

Insertion code:插入代码:

var layouts = new List<LayoutModel>
            {
                new LayoutModel { VenueId = 1, Description = "First layout" },
                new LayoutModel { VenueId = 1, Description = "Second layout" },
            };
            _context.Layouts.AddRange(layouts);
            _context.SaveChanges();

I'm not allowed to use navigation properties我不允许使用导航属性

Id column or property is marked as identity column in the definition of LayoutViewModel id 列或属性在 LayoutViewModel 的定义中被标记为标识列

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }

So, no need to assign it explicitly as it will be populated by Database automatically after the row is inserted into Layout table.因此,无需显式分配它,因为在将行插入到布局表中后,它将由数据库自动填充。 Please update your layouts population as below to remove Id assignment:请更新您的布局人口如下以删除 ID 分配:

var layouts = new List<LayoutModel> { 
    new LayoutModel { /*Id = 1,*/ VenueId = 1, Description = "First layout" }, 
    new LayoutModel { /*Id = 2, */ VenueId = 1, Description = "Second layout" }
};
// code smell
foreach(var layout in layouts)
{
     context.Entry(layout).State = EntityState.Added;
}
_context.Layouts.AddRange(layouts);
_context.SaveChanges();

Also, please update your LayoutModel as below:另外,请更新您的 LayoutModel 如下:

public class LayoutModel
{
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
     [Key]
     [Column(Order = 0)]
     public int Id { get; set; }

     [Key]
     [Column(Order = 1)]
     //[ForeignKey("Venue")]
     [DatabaseGenerated(DatabaseGeneratedOption.None)]
     public int VenueId { get; set; }

     //public virtual VenueModel Venue { get; set; } //Please correct Venue property type
}

Also, please verify whether Venue is loaded into _context.Layouts or not.另外,请验证 Venue 是否已加载到 _context.Layouts 中。

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

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