我是实体框架的新手,我正在尝试使用 POCOs 。大多数教程似乎数据库优先,生成代码或代码优先 POCOs 。使用POCO处理数据库优先的(尽管有一些)并不多。
我的设置: 我试图在一个有点大的现有项目中使用EF。仅仅是我的问题,我尝试了以下设置。
我的项目包含一个 EDMX模型,连接到本地数据库上的单个表。接下来,我将 Model.Designer.cs 中的生成代码复制到另一个 .cs 文件。然后,我将代码生成策略设置为 无 。然后我创建一个上下文类,如下所示。
public class LocalDB : ObjectContext
{
public const string ConnectionString = "name=LocalEntities";
public const string ContainerName = "LocalEntities";
public ObjectSet<Product_Listing> OpenList;
public LocalDB() : base(ConnectionString, ContainerName)
{
OpenList = CreateObjectSet<Product_Listing>(); //InvalidOperationException!!
}
}
问题: 当我点击构造函数时,我得到以下异常:
InvalidOperationException 。“无法找到EntityType'EFFTrial.LocalAccess.Product_Listing的映射和元数据信息。”
我会感激任何帮助。我所拥有的书(由Lerman提供)与EF-4有关,但我认为我在VS 2010上的代码和.Net 4支持EF-6。正如我上面提到的,我是新手,所以只要没有.Net 4.5,我就不会设置版本。
答案 0 :(得分:4)
为自己省点麻烦:
http://visualstudiogallery.msdn.microsoft.com/ee4fcff9-0c4c-4179-afd9-7a2fb90f5838
EntityFramework反向POCO生成器
OR
http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d
实体框架Power Tools Beta 4
逆向工程师代码优先 - 为现有数据库生成POCO类,派生DbContext和Code First映射。
===========================================
在您离开之前运行它...并检查您的屏幕保护程序设置。 (Aka,可能需要一段时间,特别是“电动工具”。
===========================================
这是一个Northwind“客户”示例。也许你可以把它映射到你的桌子上。
namespace NorthWindyDataLayer.Models
{
[Serializable]
public partial class Customer
{
public Customer()
{
}
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
}
}
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
namespace NorthWindyDataLayer.Models
{
public partial class WindyContext : DbContext
{
static WindyContext()
{
//Database.SetInitializer<WindyContext>(null);
}
public WindyContext()
: base("Name=NorthwindContext")
{
}
public DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CustomerMap());
}
}
}
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
namespace NorthWindyDataLayer.Models.Mapping
{
public class CustomerMap : EntityTypeConfiguration<Customer>
{
public CustomerMap()
{
// Primary Key
this.HasKey(t => t.CustomerID);
// Properties
this.Property(t => t.CustomerID)
.IsRequired()
.IsFixedLength()
.HasMaxLength(5);
this.Property(t => t.CompanyName)
.IsRequired()
.HasMaxLength(40);
this.Property(t => t.ContactName)
.HasMaxLength(30);
this.Property(t => t.ContactTitle)
.HasMaxLength(30);
this.Property(t => t.Address)
.HasMaxLength(60);
this.Property(t => t.City)
.HasMaxLength(15);
this.Property(t => t.Region)
.HasMaxLength(15);
this.Property(t => t.PostalCode)
.HasMaxLength(10);
this.Property(t => t.Country)
.HasMaxLength(15);
this.Property(t => t.Phone)
.HasMaxLength(24);
this.Property(t => t.Fax)
.HasMaxLength(24);
// Table & Column Mappings
this.ToTable("Customers");
this.Property(t => t.CustomerID).HasColumnName("CustomerID");
this.Property(t => t.CompanyName).HasColumnName("CompanyName");
this.Property(t => t.ContactName).HasColumnName("ContactName");
this.Property(t => t.ContactTitle).HasColumnName("ContactTitle");
this.Property(t => t.Address).HasColumnName("Address");
this.Property(t => t.City).HasColumnName("City");
this.Property(t => t.Region).HasColumnName("Region");
this.Property(t => t.PostalCode).HasColumnName("PostalCode");
this.Property(t => t.Country).HasColumnName("Country");
this.Property(t => t.Phone).HasColumnName("Phone");
this.Property(t => t.Fax).HasColumnName("Fax");
}
}
}