EF 5.0(数据库优先模型)不适用于一个表 [英] EF 5.0 (Database-First Model) not working on one table
问题描述
我有一个名为TBLFIRM的数据库表。我正在使用acc数据库优先模型。 EF准备的SQL错误。
I have a database table called TBLFIRM. I am using acc database-first model. EF prepares the sql wrong. What can be wrong on this object?
谢谢
我的语法,一个非常简单的调用:
My Syntax, a very simple call:
TBLFIRM d = VT.TBLFIRMs.FirstOrDefault(p => p.ID > 0);
EF 5准备的SQL语法
The SQL syntax that EF 5 prepares
SELECT TOP (1)
[Extent1].[ID] AS [ID],
[Extent1].[DISTID] AS [DISTID],
[Extent1].[SESSIONKEY] AS [SESSIONKEY],
[Extent1].[NAME] AS [NAME],
[Extent1].[PHONE] AS [PHONE],
[Extent1].[EMAIL] AS [EMAIL],
[Extent1].[CREATEUSER] AS [CREATEUSER],
[Extent1].[CREATEDATE] AS [CREATEDATE],
[Extent1].[UPDATEUSER] AS [UPDATEUSER],
[Extent1].[UPDATEDATE] AS [UPDATEDATE],
[Extent1].[AUTHORIZEDUSERNAME] AS [AUTHORIZEDUSERNAME],
[Extent1].[AUTHORIZEDUSEREMAIL] AS [AUTHORIZEDUSEREMAIL],
[Extent1].[AUTHORIZEDUSERPHONE] AS [AUTHORIZEDUSERPHONE],
[Extent1].[SENDEMAIL] AS [SENDEMAIL],
[Extent1].[REPLYEMAIL] AS [REPLYEMAIL],
[Extent1].[ACTIVE] AS [ACTIVE],
[Extent1].[CANLOGIN] AS [CANLOGIN],
[Extent1].[INTERNALNAME] AS [INTERNALNAME],
[Extent1].[TBLDISTRIBUTOR_ID] AS [TBLDISTRIBUTOR_ID]
FROM [dbo].[TBLFIRMs] AS [Extent1]
WHERE [Extent1].[ID] > 0
上下文类
public DbSet<TBLFIRM> TBLFIRMs { get; set; }
TBLFirm类
public partial class TBLFIRM
{
public TBLFIRM()
{
this.TBLFIRMSETTINGS = new HashSet<TBLFIRMSETTING>();
this.TBLFIRMSMTPs = new HashSet<TBLFIRMSMTP>();
this.TBLMAILFIRMMATCHes = new HashSet<TBLMAILFIRMMATCH>();
this.TBLMAILGROUPs = new HashSet<TBLMAILGROUP>();
this.TBLMAILTEMPLATEs = new HashSet<TBLMAILTEMPLATE>();
this.TBLSCHEDULEs = new HashSet<TBLSCHEDULE>();
this.TBLUSERGROUPs = new HashSet<TBLUSERGROUP>();
this.TBLUSERS = new HashSet<TBLUSER>();
}
public int ID { get; set; }
public int DISTID { get; set; }
public string SESSIONKEY { get; set; }
public string NAME { get; set; }
public string PHONE { get; set; }
public string EMAIL { get; set; }
public string CREATEUSER { get; set; }
public Nullable<System.DateTime> CREATEDATE { get; set; }
public string UPDATEUSER { get; set; }
public Nullable<System.DateTime> UPDATEDATE { get; set; }
public string AUTHORIZEDUSERNAME { get; set; }
public string AUTHORIZEDUSEREMAIL { get; set; }
public string AUTHORIZEDUSERPHONE { get; set; }
public string SENDEMAIL { get; set; }
public string REPLYEMAIL { get; set; }
public Nullable<bool> ACTIVE { get; set; }
public Nullable<bool> CANLOGIN { get; set; }
public string INTERNALNAME { get; set; }
public virtual TBLDISTRIBUTOR TBLDISTRIBUTOR { get; set; }
public virtual ICollection<TBLFIRMSETTING> TBLFIRMSETTINGS { get; set; }
public virtual ICollection<TBLFIRMSMTP> TBLFIRMSMTPs { get; set; }
public virtual ICollection<TBLMAILFIRMMATCH> TBLMAILFIRMMATCHes { get; set; }
public virtual ICollection<TBLMAILGROUP> TBLMAILGROUPs { get; set; }
public virtual ICollection<TBLMAILTEMPLATE> TBLMAILTEMPLATEs { get; set; }
public virtual ICollection<TBLSCHEDULE> TBLSCHEDULEs { get; set; }
public virtual ICollection<TBLUSERGROUP> TBLUSERGROUPs { get; set; }
public virtual ICollection<TBLUSER> TBLUSERS { get; set; }
}
推荐答案
经过更深入的研究后,我通过BRian Rogers @ 代码优先与数据库优先的回复,能够找到并解决问题。
After a deeper research I was able to find and fix the problem thanks to a reply by BRian Rogers @ Code First vs. Database First.
如果您首先使用数据库或模型,则连接字符串必须具有元数据,否则EF将此解释为代码优先方法。
If you are approaching Database or Model First the the connection string must have a metadata, otherwise EF interprets this as Code First approach.
当我从网站调用包含EF对象的库时,web.config的连接字符串是一个纯数据库连接。
As I was calling the library holding EF objects from a web site, the web.config's connectionstring was a plaing database connection.
<add name="EmmContext" connectionString="Data Source=.;Initial Catalog=GPMM;Persist Security Info=True;User ID=********;Pwd=******" providerName="System.Data.SqlClient"/>
更改为
<add name="EmmContext" connectionString="metadata=res://*/DAL.DBModel.csdl|res://*/DAL.DBModel.ssdl|res://*/DAL.DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=GPMM;persist security info=True;user id=*******;password=********;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>
它就像一种魅力。
只是想分享这种特殊的经验。谢谢Brian Rogers! :-)
Just wanted to share this particular experience. Thank you Brian Rogers! :-)
这篇关于EF 5.0(数据库优先模型)不适用于一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!