Reputation: 2013
I am new to Entity Framework, and I am trying to work with POCOs. Most of the Tutorials seem to be Database First with Generated Code OR Code First with POCOs. There are not many (though there are a few) that deal with Database First with POCOs.
My Setup: I am trying to use EF in an existing project that is a bit large. To simply my question, I have attempted the following setup.
I have a project containing a single EDMX Model, connecting to a single table on a Local DB. Next, I copied the Generated Code from the Model.Designer.cs to another .cs file in the Project. I then set the Code Generation Strategy to None. I then create a Context Class as below.
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!!
}
}
Problem: When I hit the constructor, I get the following exception:
InvalidOperationException.“Mapping and metadata information could not be found for EntityType 'EFFTrial.LocalAccess.Product_Listing.”
I’d be grateful for any help. The book I have (by Lerman) is related to EF-4, but my code on VS 2010, and .Net 4 supports EF-6, I think. As I mentioned above, I am new, so I am not set on the versions as long as I can get by without .Net 4.5.
Upvotes: 1
Views: 818
Reputation: 27904
Save yourself some trouble :
http://visualstudiogallery.msdn.microsoft.com/ee4fcff9-0c4c-4179-afd9-7a2fb90f5838
EntityFramework Reverse POCO Generator
OR
http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d
Entity Framework Power Tools Beta 4
Reverse Engineer Code First - Generates POCO classes, derived DbContext and Code First mapping for an existing database.
===========================================
Run it before you leave for the evening..and check your screen saver settings. (Aka, it could take a while, esp the "Power Tools" one.
===========================================
Here is a Northwind "Customer" example. Perhaps you can map it to your table.
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");
}
}
}
Upvotes: 4