Why is the TableAttribute in the Entity Framework Dll?

坚强是说给别人听的谎言 提交于 2019-12-10 06:37:22

问题


Is there a good reason why the Table attribute (which can be used to map a POCO class to the correct database name/schema) is in the EntityFramework.dll?

Doesn't this prevent you from creating a Domain project that simply contains your entities with no dependency on a specific data access technology? For example, if I use this attribute I don't believe the classes would be portable to Silverlight.

Is this an oversight, or am I missing something?

I realize I could use the fluent API to circumvent this, but the attribute seems preferable for this purpose.


回答1:


I think because the TableAttribute has been added in EF 4.1. It belongs to namespace System.ComponentModel.DataAnnotations and would probably have been added to System.ComponentModel.DataAnnotations.dll assembly if EF 4.1 had been part of a regular .NET Framework release. But because EF 4.1 was released independently from a Framework update they couldn't touch the framework core assemblies. So, it is for now in EntityFramework.dll but still in System.ComponentModel.DataAnnotations namespace, so somehow independent from Entity Framework. Maybe it will be moved into System.ComponentModel.DataAnnotations.dll with the next .NET Framework version.

For now, if you want to decorate your POCOs with the TableAttribute you must reference EntityFramework.dll. I wouldn't consider this as dependency from Entity Framework as long as you don't use the "real" EF stuff (DbContext, etc.) in your custom assembly.




回答2:


This does cause a problem for Silverlight.

For those with struggling with this one you must, for the moment, use the fluent API to do the mapping.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<BankAccount>().ToTable("BankAccounts");
    modelBuilder.Entity<CreditCard>().ToTable("CreditCards");
}

HTH.



来源:https://stackoverflow.com/questions/6034041/why-is-the-tableattribute-in-the-entity-framework-dll

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