开发者

Why is the TableAttribute in the Entity Framework Dll?

开发者 https://www.devze.com 2023-03-06 18:42 出处:网络
Is there a good reason why the Table attribute (which can be used to map a POCO class to the correct database name/schem开发者_如何学JAVAa) is in the EntityFramework.dll?

Is there a good reason why the Table attribute (which can be used to map a POCO class to the correct database name/schem开发者_如何学JAVAa) 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号