gpt4 book ai didi

.net - 如何使用流畅的 API 添加主键 - Entity Framework EF 4.x Code First

转载 作者:太空狗 更新时间:2023-10-30 01:55:06 25 4
gpt4 key购买 nike

我需要映射一些自动生成的类,出于明显的原因我不想更改它们(它是自动生成的!)。

那么是否有可能使用 Entity Framework 的流畅 API(在我的例子中是 4.3)定义主键?由于 NULL 值,将属性作为 ComplexType 处理会导致 DbUpdateExcetion。在生成的类中添加主键可能是一种解决方案,但不适合我,POCOs 必须保持不变。请帮助!谢谢。

目前我忽略了属性,因为...

ModelValidationException 
EntityType 'Attribute' has no key defined. Define the key for this EntityType.
EntitySet 'Attributes' is based on type 'Attribute' that has no keys defined.

这是一个简短的示例代码:

public class Item
{
public ID {set;get;}
public Attribute[] Attributes {set;get;}
}
public class Attribute
{
public SomeComplexType misc1 {set; get;}
public SomeComplexType misc2 {set; get;}
}

public class ItemDb : DbContext
{
public ItemDb(string connection) : base(connection) {}

public DbSet<Item> Items { get; set; }

protected override void OnModelCreating(DbModelBuilder builder)
{
// currently I am ignoring the Attributes
builder.Ignore<Attributes>();
}
}

最佳答案

您可以使用 HasKey方法。

builder.Entity<FooBar>().HasKey(...);

关于.net - 如何使用流畅的 API 添加主键 - Entity Framework EF 4.x Code First,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10719543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com