使用 Entity Framework 4.1 Codefirst,如何在 poco 类中创建未映射的字段?
我有一组类作为我的域对象。 我有一组配置文件来映射这些对象(EntityTypeConfiguration<>)。
当我向任何域对象添加属性而不映射到列时,dbcontext 尝试查询该列,忽略它未映射的事实。
我必须在配置代码或 dbcontext 代码中缺少设置。我不想向 poco 类添加属性(装饰 pocos 将它们绑定到特定的持久性实现,我希望避免这种情况)。
在调用 IQueryable 来填充票证对象时,调用失败并显示消息:
列名“NotInDatabase”无效。
public class Ticket
{
public Ticket()
{
}
public virtual int Id
{
get;
set;
}
public virtual string Title
{
get;
set;
}
public virtual string Description
{
get;
set;
}
public string NotInDatabase
{
get;
set;
}
}
internal class TicketConfiguration : EntityTypeConfiguration<Ticket>
{
public TicketConfiguration()
{
ToTable("ticket_table_name");
HasKey(o => o.Id)
.Property(o => o.Id)
.HasColumnName("ticketId")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.IsRequired();
Property(o => o.Title).HasColumnName("TicketTitle");
Property(o => o.Description).HasColumnName("TicketDescription");
}
}
笔记: 请不要建议针对我的情况使用“数据库优先”或“模型优先”。我想首先使用代码的功能将 poco 对象映射到数据库,即使我有现有的数据库结构。我正在将其与 nhibernate 进行比较,并且确实希望坚持类似的结构(由于 Microsoft“采用”了流畅的 nhibernate 方法,因此很容易进行同类比较)。
谢谢!
I have a set of classes as my domain objects.
I have a set of configuration files to map these objects (EntityTypeConfiguration<>).
When I add a property to any of the domain objects without mapping to a column, the dbcontext attempts to query for the column, ignoring the fact that it is not mapped.
I must be missing a setting either in the configuration code or the dbcontext code. I do not want to add an attribute to the poco class (decorating the pocos tie them to a specific persistence implementation, which I wish to avoid).
On the call against the IQueryable to populate a ticket object, the call fails with the message:
Invalid column name 'NotInDatabase'.
public class Ticket
{
public Ticket()
{
}
public virtual int Id
{
get;
set;
}
public virtual string Title
{
get;
set;
}
public virtual string Description
{
get;
set;
}
public string NotInDatabase
{
get;
set;
}
}
internal class TicketConfiguration : EntityTypeConfiguration<Ticket>
{
public TicketConfiguration()
{
ToTable("ticket_table_name");
HasKey(o => o.Id)
.Property(o => o.Id)
.HasColumnName("ticketId")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.IsRequired();
Property(o => o.Title).HasColumnName("TicketTitle");
Property(o => o.Description).HasColumnName("TicketDescription");
}
}
Note:
Please do not suggest using "Database First" or "Model First" for my situation. I want to map poco objects to the database using the features of code first, even though I have an existing db structure. I am comparing this to nhibernate and really want to stick to a similar structure (since Microsoft "adopted" fluent nhibernate's approach, it's pretty easy to compare apples to apples).
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.Ignore 应该可以解决问题,或者通过属性将其称为 [NotMapped]
.Ignore should do the trick or by attribute it's called [NotMapped]