[英]c# Entity Framework “Cannot resolve symbol”
I created a sample project using entity framework 4. It was fairly simple, I created a new data model by choosing EF Designer from database. 我使用实体框架4创建了一个示例项目。这非常简单,我通过从数据库中选择EF Designer创建了一个新的数据模型。 Created the data connection and selected all tables from my database.
创建数据连接并从数据库中选择所有表。 It seems fine - I am able to see the tables in the designer.
看起来不错-我可以在设计器中看到表格。
The problem is that then I can't add or delete objects. 问题是我无法添加或删除对象。 My db class looks like that:
我的数据库类看起来像这样:
namespace DBAccess
{
public class DatabaseAccess : IDisposable
{
private MyDBModel dbModel;
public DatabaseAccess ()
{
dbModel= new MyDBModel();
}
public Accounts AddAccount()
{
var A = new Accounts();
dbModel.Accounts.AddObject(a); // here's the "Cannot resolve symbol 'AddObject'" error
}
public void DeleteAccount(Accounts a)
{
dbModel.DeleteObject(a); // here's the "Cannot resolve symbol 'DeleteObject'" error
}
}
}
Why can't I use these methods? 为什么我不能使用这些方法? I've used them before in other projects..
我以前在其他项目中使用过它们。
This is the connection string in the App.config
file: 这是
App.config
文件中的连接字符串:
<connectionStrings>
<add name="MyDBModel" connectionString="metadata=res://*/MyDBModel.csdl|res://*/MyDBModel.ssdl|res://*/MyDBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=MyServer;initial catalog=MyDB;persist security info=True;user id=MyUser;password=MyPassword;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Okay, I found a solution, although I'm not sure if it's the right one.. 好的,我找到了解决方案,尽管我不确定这是否合适。
I found this article on Microsoft's website http://msdn.microsoft.com/en-us/data/jj556581.aspx 我在Microsoft网站http://msdn.microsoft.com/zh-cn/data/jj556581.aspx上找到了这篇文章
The settings on my data model were different. 我的数据模型上的设置不同。 In
Code Generation Strategy
I have T4
and Legacy ObjectContext
. 在
Code Generation Strategy
我具有T4
和Legacy ObjectContext
。 It was set to T4
and I changed it to Legacy ObjectContext
. 它设置为
T4
,我将其更改为Legacy ObjectContext
。 Then under the .edmx
file I delete the two files with .tt
extensions. 然后,在
.edmx
文件下,删除两个扩展名为.tt
文件。 In my case they were MyDBModel.Context.tt
and MyDBModel.tt
. 就我而言,它们是
MyDBModel.Context.tt
和MyDBModel.tt
。
Rebuilt the whole solution and it works now. 重建了整个解决方案,现在就可以使用。
I hope it will help to someone. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.