Getting POCO mapping error in Entity Framework
I feel like I'm missing some basic setup step here, but I've been wading through forum posts, blog articles, and videos for hours and keep having the same problem, so I figure it's time to post.
The error I'm getting is: Mapping and metadata information could not be found for EntityType 'qTrade.BusinessLayer.Domain.Model.Audit.LoanPool'.
I've got an Entity Model file called Audit.edmx that has code generation turned off. In there I generated an Entity from the database for this table (pasted from the create script, LoanPoolAuditId is the Primary Key):
CREATE TABLE [Audit].[LoanPool](
[Type] [char](1) NULL,
[TableName] [varchar](128) NULL,
[PK] [varchar](1000) NULL,
[FieldName] [varchar](128) NULL,
[OldValue] [varchar](1000) NULL,
[NewValue] [varchar](1000) NULL,
[UpdateDate] [datetime] NULL,
[UserName] [varchar](128) NULL,
[CommonID] [int] NULL,
[LoanPoolAuditId] [int] IDENTITY(1,1) NOT NULL,
I've got a class I created to be a POCO elsewhere in the same project
public class LoanPool
{
public int CommonID { get; set; }
public string FieldName { get; set; }
public int LoanPoolAuditId { get; set; }
public string NewValue { get; set; }
public string OldValue { get; set; }
public string PK { get; set; }
public string TableName { get; set; }
public char Type { get; set; }
public DateTime UpdateDate { get; set; }
public string UserName { get; set; }
}
And I've got the context class
public class AuditContext : ObjectContext
{
public AuditContext()
: base("name=AuditEntities", "AuditEntities")
{
this.LoanPools = CreateObjectSet<LoanPool>();
}
public ObjectSet<LoanPool> LoanPools { get; set; }
}
Heres the connection string for reference
"metadata=res://*/EntityModels.Audit.csdl|res://*/EntityModels.Audit.ssdl|res://*/EntityModels.Audit.msl;provider=System.Data.SqlClient;provider connection string="data source=serverName;initial catalog=databaseName;persist security info=True;user id=*SNIP*;password=*SNIP*;multipleactiveresultsets=True;App=EntityFramework""
Very simple (or so I thought).
When I go to instantiate an instance of the context I get the aforementioned error on the CreateObjectSet step.
Any suggestions would be greatly appreciated, even if they are simple or obvious, thi开发者_StackOverflows is my first time using POCOs so there's a good chance I missed something.
Go figure I had no trouble using code-first in a different project :-P
Thanks
We generally recommend using DbContext as the primary means of operating with anything EF 4.1 or beyond. In this case you would need to get rid of your existing .edmx and modify your AuditContext as follows:
public class AuditContext : DbContext
{
public DbSet<LoanPool> LoanPools { get; set; }
}
You can also see the corresponding walkthrough on the official blog here: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx. Also, you will need to put the [Key] attribute on LoanPoolAuditId as it does not conform to the expected conventions. That's also mentioned in the blog post.
HTH, Mark
精彩评论