我最近开始使用EF v4 Code Only库来处理我正在进行的一些项目.但是,我遇到了一些障碍.我似乎无法弄清楚连接字符串的正确格式.我使用以下代码来构建连接字符串:
string connectionString = new EntityConnectionStringBuilder
{
Provider = "System.Data.SqlClient",
ProviderConnectionString = new SqlConnectionStringBuilder
{
DataSource = "localhost",
InitialCatalog = "ASM_Testing",
IntegratedSecurity = true,
Pooling = false
}.ConnectionString
}.ConnectionString;
Run Code Online (Sandbox Code Playgroud)
但是,使用它会导致以下错误:
Specifications_for_EntityContext.When_logging_in_application_with_valid_app_role_and_password.Login_should_be_successful : System.ArgumentException : Some required information is missing from the connection string. The 'metadata' keyword is always required.
Stack Trace:
at System.Data.EntityClient.EntityConnection.ValidateValueForTheKeyword(DbConnectionOptions effectiveConnectionOptions, String keywordName)
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.Objects.ObjectContext..ctor(String connectionString)
at ASM.Data.EntityContext..ctor(String connectionString) in C:\Users\Jon Rista\TFS\Advanced Service Management\Trunk\Main\Source\ASM.Data\EntityContext.cs:line 16
at Specifications_for_EntityContext.Behaves_like_EntityContext_connected_to_a_database.TestableEntityContext..ctor(String connectionString) in C:\Users\Jon Rista\TFS\Advanced …Run Code Online (Sandbox Code Playgroud) 我正在开发ASP.NET应用程序,我有EF 4.0模型的问题.
EF模型检测新添加和删除的数据,但不检测数据库中的修改数据.
这是我所拥有的问题的一个例子.
A-数据库:
用于生成"Employees"数据库表的脚本
CREATE TABLE [dbo].[Employees]
(
[id] [int] IDENTITY(1, 1)
NOT NULL,
[name] [nvarchar](50) NULL,
CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED ( [id] ASC )
WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
)
ON [PRIMARY]
Run Code Online (Sandbox Code Playgroud)
B-应用:
以下是示例项目的链接Click Here.
重现错误的步骤:
1-创建数据库并运行脚本以创建表.
2-在employees表中插入测试数据,然后运行该应用程序.数据将加载到默认页面中.
3-更改连接字符串并运行应用程序.
3-更新数据库中的某些值(直接形成sql).并刷新页面
您会发现应用程序仍然显示旧数据,而如果您从表中添加或删除项目,则分别在视图中添加或删除它.
在此先感谢您的帮助.
我有一张Property桌子,另一张Detail桌子.我使用断开连接的方法,其中添加了多个属性,然后当我们单击保存按钮时,它将保存对数据库的更改.这种不为每个添加的属性保存的方法是因为用户也可以从列表中删除.
这就是我在做什么
foreach (Property P in Results)
{
if(P.PropertyId==0 && P.EntityState==EntityState.Added)
Repository.Properties.AddObject(P);
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试保存数据时
public void Save()
{
Repository.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
它返回错误
无法确定'database.FK_Details_Property'
关系的主要结尾.多个添加的实体可以具有相同的主键.
我认为它是因为每次我调用AddObject时它的主键是0,我不知道什么可以解决这个问题.对于遇到它的人来说,这是否相似或听起来很熟悉?
提前致谢
为什么在EF4,Linq2SQL或任何其他数据映射技术中支持POCO如此重要?我理解OO意义上的POCO的概念,但是当涉及到ORM时,我还缺少其他东西吗?
编辑:我只是在ORM的上下文中添加我对POCO的个人定义:它是由开发人员手工编码的类,而不是由ORM映射工具生成,扩充或注释的类(如Visual Studio的EF4设计师).
如果我错了,请纠正我.
我有一节课:
public class classParty
{
private int _arrivedCount;
public int PartyID {get; private set;}
public DateTime PartyDate {get; private set;}
public int ArrivedCount
{
get
{
return _arrivedCount;
}
set
{
_arrivedCount = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以映射PartyId和PartyDate但是我没有ArrivedCount的列(它是时间计数的一个时刻,它不会持续存在).
如何告诉EF 4.1停止查找名为"ArrivedCount"的列?它不在桌子上.它不会出现在桌子上.它只是对象的一个属性,而这就是全部.
提前致谢.
编辑:这是classParty的Fluent API配置.
public class PartyConfiguration : EntityTypeConfiguration<classParty>
{
public PartyConfiguration()
: base()
{
HasKey(p => p.PartyID);
Property(p => p.PartyID)
.HasColumnName("PartyID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.IsRequired();
Property(p => p.PartyDate)
.HasColumnName("PartyDate")
.IsRequired();
ToTable("Party");
}
}
Run Code Online (Sandbox Code Playgroud) 我一直在尝试将LINQ表达式重构为一个方法,并且已经遇到了" Internal .NET Framework Data Provider error 1025."和" The parameter 'xyz' was not bound in the specified LINQ to Entities query expression."异常.
以下是实体模型的相关部分(使用EF 4.2/LINQ to Entities):
public class Place : Entity
{
public string OfficialName { get; protected internal set; }
public virtual ICollection<PlaceName> { get; protected internal set; }
}
public class PlaceName : Entity
{
public string Text { get; protected internal set; }
public string AsciiEquivalent { get; protected internal set; }
public virtual Language …Run Code Online (Sandbox Code Playgroud) linq-to-entities expression-trees predicatebuilder entity-framework-4 linqkit
我正在使用ODP.NET 11.2.0.3.0的Entity Framework.我得到了持久性工作的一些表,但这一个平坦拒绝添加一个简单的新对象,我无法弄明白.非常感谢您的帮助.谢谢
这是代码.我通过模型浏览器从对象中删除了图像字段,并且在调用SaveChanges()时代码仍然失败.
var corpDirectoryEntities = new CorpDirectoryEntities();
var cc = new EmployeePhoto();
cc.UserId = 12345; // NUMBER field
cc.ImageName = "imagename"; // VARCHAR2(100)
cc.Image = photoStream; // LONG RAW
corpDirectoryEntities.EmployeePhotos.AddObject(cc);
corpDirectoryEntities.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
发生以下异常.
System.Data.UpdateException was unhandled by user code
Message=An error occurred while updating the entries. See the inner exception for details.
Source=System.Data.Entity
StackTrace:
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Objects.ObjectContext.SaveChanges()
at Repositories.Services.CorpDirectory.CorpDirectoryRepository.SaveDirectoryAccountPhoto(Int32 accountId, Byte[] photoStream) in C:\Dev\Projects\Repositories\Services\CorpDirectory\CorpDirectoryRepository.cs:line 356
at Tests.CorpDirectory.CorpDirectoryUserTestFixture.TestGetUserPhoto() in C:\Dev\Projects\Repositories.Tests\CorpDirectory\CorpDirectoryUserTestFixture.cs:line … 我使用EF 4,我DateTimeStart在我的实体中有一个属性,在这种格式中有date 16/08/2012 08:14:40,我想用EF查询并查找所有实体within the date 16/08/2012 only.使用下面代码我收到此错误
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Run Code Online (Sandbox Code Playgroud)
我的代码
DateTime dateTimeNow = DateTime.UtcNow;
DateTime dateNow = dateTimeNow.Date;
return db.EventCustoms.Where(x => x.DataTimeStart.Date <= dateNow)
.Select(y => new { y.EventId, y.EventTitle, y.DataTimeStart });
Run Code Online (Sandbox Code Playgroud) 我的web.config文件中当前有连接字符串.
是否可以将其放在单独的文件中并将实体框架指向它.
我必须一个接一个地重新计算大量实体的值.
在此过程中,所有自跟踪实体都在同一个ObjectContext中进行更改.对于需要处理的每个实体,必须从数据库中提取少量数据.这导致了许多相同的SQL查询,但使用了不同的参数.
我正在使用Solutions Design的ORM Profiler软件来分析发送到数据库的查询.
查询本身对我来说似乎没问题.它们很短,不需要太多时间来执行.
但是,我对分析器如何显示查询的实际处理方式感到困惑:

如您所见,它保持打开和关闭相同的数据库连接.
现在,看一下单个Open/Query/Close连接的时间:

看起来打开和关闭数据库连接会浪费时间.
看完这个答案后,我改变了我的代码所以现在它看起来像这样:
using (var connection = new EntityConnection(ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString))
{
using (var context = new MyEntities(connection))
{
// ...
Run Code Online (Sandbox Code Playgroud)
我现在可以看到它仍然使用相同的连接(这很好),但是,连接仍然在查询之间保持关闭和打开.
Gert Arnold建议我在使用上下文之前明确打开连接.然后我修改了我的代码,看起来像这样:
using (var connection = new EntityConnection(ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString))
{
connection.Open();
using (var context = new MyEntities(connection))
{
// ...
Run Code Online (Sandbox Code Playgroud)
现在它有效!每个查询都发送到同一个数据库连接:

我现在很好奇为什么在使用上下文之前需要打开连接?
c# database-connection entity-framework-4 self-tracking-entities