草庐IT

c# - Entity Framework 给出异常 : "The underlying provider failed on Open."

我有一个测试。发生的情况是,无论何时首先执行test1,test2都会失败并显示消息:"System.Data.EntityException:System.Data.EntityException:theunderlyingproviderfailedonopen.cannotopendatabase"DBEntities"requestedbythelogin.Theloginfailed.".只要先执行test2,test1就会失败并显示相同的消息。过去3天我一直在处理这个问题。[TestClass]classMyTestClass{DBEntitiesdb;[TestIniti

c# - 在 Entity Framework 中使用唯一约束交换值

我对Navigations表的名为Index的列有唯一约束。我有两个Navigation实体,我想交换它们的Index值。当我调用db.SaveChanges时,它会抛出一个异常,表明违反了唯一约束。似乎EF正在更新一个值,然后更新另一个值,因此违反了约束。难道不应该在事务中同时更新它们,然后在整理出值且不违反约束后尝试提交吗?有没有不使用临时值的方法解决这个问题? 最佳答案 这不是EF的问题,而是SQL数据库的问题,因为update命令是顺序执行的。事务与此无关——所有约束都是按命令而不是按事务验证的。如果您想交换唯一值,您需要更

c# - 如何从 Entity Framework 6 中的 Auditlog 实体获取 ID

我知道那里有几篇类似的帖子,但我找不到任何解决此问题的帖子。我想在EntityFramework6中添加、更改或删除实体(软删除)时添加(某种)AudioLog。我已经覆盖了SaveChanges,因为我只想为添加、修改的EntityStates添加日志条目或已删除,我会在第一次调用SaveChanges之前获取列表。问题是,因为我需要记录已执行的操作,所以我需要检查实体的EntityState。但是在调用SaveChanges之后,所有条目的EntityState都保持不变。publicoverrideintSaveChanges(){using(varscope=newTransa

c# - 如何在 Entity Framework Code First 中表示 Bridge 表

我正在尝试找出如何表示两个实体(多对多关系)之间的桥接表我首先使用实体​​框架代码Student:StudentIDintPKStudentNameVARCHAR(50)Class:ClassIDintPKClassNameVARCHAR(50)StudentClass:StudentIDINTPKClassIDINTPK我应该在EntityFrameworkCodeFirst中使用什么最好的类结构来表示它,我如何从桥接表中选择并插入其中。我应该像这样表示类吗:publicclassStudent{publicintStudentId{get;set;}publicstringStud

c# - 使用声明 Entity Framework 上下文

声明EntityFramework上下文的最佳实践是什么function(){DBContextcontext=newDBContext();//Entitycodereturn;}或function(){using(DBContextcontext=newDBContext()){//Entitycode}}我们需要在EntityFrameWork中使用using吗?如果是,我的第二个问题在数据访问层中执行EF并将结果存储在IEnumerable中使用我的DLfunction(){IEnumerablesomething=null;using(DBContextcontext=new

c# - 如何使用 Entity Framework/LINQ 从特定表中提取元组列表?

我需要使用EntityFramework从C#.NET中的一个大表中提取一对“ID”/“名称”的列表。我试试这个请求:List>list=(fromresindb.ResourceselectnewTuple(res.Resource_ID,res.Name)).ToList();但不幸的是我遇到了这个错误:OnlyparameterlessconstructorsandinitializersaresupportedinLINQtoEntities.我不明白如何使用这个框架提取这个元组列表,我对这个错误感到有点迷茫。你能帮助我理解和解决我的问题吗?最好的问候,亚历克斯

c# - 运行代码首先迁移更新数据库时出错

我在迁移到我的数据库时遇到了问题,我似乎无法找到我遇到的错误的答案System.MissingMethodException:Methodnotfound:'System.Data.Entity.Migrations.Builders.TableBuilder`1System.Data.Entity.Migrations.Builders.TableBuilder`1.Index(System.Linq.Expressions.Expression`1>,Boolean,Boolean,System.Object)'.atEvento.Migrations.initialcreate.

c# - 如何从 Entity Framework 更新主键?

我有tableeventidint--notPKkeybutwithautoincrementjobid--PKautoincrementdisableduseridint--PKautoincrementdisabled要更新jobID,我执行以下操作:varitemforupdate=context.table.where(n=>n.eventid==someparameter).FirstorDefault()我从数据库中正确获取了项目,但是在分配时:itemforupdate.jobID=5;context.SaveChanges();在context.SaveChanges(

c# - 如何最小化 Entity Framework SaveChangesAsync?

MockdbContext;[TestFixtureSetUp]publicvoidSetupDbContext(){dbContext=newMock();dbContext.Setup(c=>c.SaveChanges()).Verifiable();dbContext.Setup(c=>c.SaveChangesAsync()).Verifiable();dbContext.Setup(c=>c.Customers.Add(It.IsAny())).Returns(It.IsAny()).Verifiable();}[Test]publicasyncTaskAddCustomer

c# - 附加类型为 'X' 的实体失败,因为相同类型的另一个实体已经具有相同的主键值

错误信息:Attachinganentityoftype'FaridCRMData.Models.Customer'failedbecauseanotherentityofthesametypealreadyhasthesameprimarykeyvalue.ThiscanhappenwhenusingtheAttach()methodorsettingthestateofanentityto'Unchanged'or'Modified'ifanyentitiesinthegraphhaveconflicting>keyvalues.Thismaybebecausesomeentiti