我的项目使用 EF Code First 运行得很好。但现在我需要在数据库中添加 1 个字段(例如 DateTime 类型的 CreateDate),该字段可以为 null。
因此,我编写了脚本以向 DB(CreateDate (datetime) null ) 添加 1 个字段,然后修改模型以包含新字段
public class Account()
{
...
public DateTime CreateDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
下面是使用 FluentApi 创建此字段选项的代码
Property(x => x.CreateDate).IsOptional();
Run Code Online (Sandbox Code Playgroud)
问题是当我尝试获取帐户的任何实例时,当我修改字段时出现错误,如下所示:
public DateTime? CreateDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
然后就可以了。
你们能给我解释一下吗?为什么我的 Fluent api 不起作用或者我做错了什么?
谢谢。
我遇到了以下“问题”(它可能只是一个功能,但这将是一个非常奇怪的问题)。使用实体框架 5,当插入实体而不通过外键指定链接实体时,EF 会自动将值分配给相关实体(如果同时插入)。
好吧,这还不是很清楚,所以这里有一些重现该行为的代码:
[TestClass]
public class TestEntityFramework
{
[TestMethod]
public void Test()
{
using (var context = new TestDataContext())
{
context.Foos.Add(new Foo { FooProp = "FooProp"});
context.Bars.Add(new Bar {BarProp = "BarProp"});
context.SaveChanges();
}
}
}
public class Foo
{
[Key]
public int Id { get; set; }
public string FooProp { get; set; }
}
public class Bar
{
[Key]
public int Id { get; set; }
public int FooId { get; set; }
[ForeignKey("FooId")]
public virtual Foo Foo …Run Code Online (Sandbox Code Playgroud) 我正在查看EntityFramework.Extended。它可以在数据库实体上运行Update()和Delete()。喜欢:
//delete all users where FirstName matches
context.Users.Delete(u => u.FirstName == "firstname");
Run Code Online (Sandbox Code Playgroud)
但有趣的是,它不需要调用context.SaveChanges(),它只是直接进入数据库并删除记录。
这是一个糟糕的设计吗?我认为对实体的每次修改都需要调用SaveChanges()才能生效并保持数据库和内存对象之间正确的关系和映射。
更深入地了解实体框架和存储库,以便更好地进行测试。想知道这是否明智?
public interface IRepository
{
int SaveChanges();
void Dispose();
}
using (MyContext context = new MyContext())
{
TransactionRepository txns = new TransactionRepository(context); // TransactionRepository implement IRepository
MappingRepository maps = new MappingRepository(context); // MappingRepositoryimplement IRepository
SomeCommand command = new SomeCommand(txns, maps);
command.Execute();
}
Run Code Online (Sandbox Code Playgroud)
每个存储库在逻辑上都是不同的,因此理论上可能位于不同的数据源中。目前,他们使用相同的数据库。每个存储库类都实现 IRepository,特别是 SaveChanges() 以及一些查询方法,为了简洁起见,我没有展示这些方法。
使用多个存储库的良好做法是什么?
Person我有带有以下字段的表(以及实体数据模型中的实体) :
Name Type
SocialID String
FirstName String
LastName String
Run Code Online (Sandbox Code Playgroud)
这SocialID是主键。我想更新SocialID每条记录的值。但是,当我尝试在实体框架中更新此字段时,出现以下错误:
The property 'SocialID' is part of the object's key information and cannot
be modified.
Run Code Online (Sandbox Code Playgroud)
我得到上述错误的代码是:
foreach (var p in Entity.Persons)
{
p.SocialID= p.SocialID + "00";
Entity.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我正在使用实体框架 codefirst 并且我有以下设计
public class Location
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsDeleted { get; set; }
}
public class DelieryRate
{
public int Id { get; set; }
public int OriginId { get; set; }
public Location Origin { get; set; }
public int DestinationId { get; set; }
public Location Destination { get; set; }
public double Amount { get; set; }
public bool IsActive { get; …Run Code Online (Sandbox Code Playgroud) 我正在使用实体框架 5,并且尝试删除没有主键的实体。我的意思是我的表甚至没有主键。
场景如下:
我有 3 个表,其流程如下:
MainTeam -> UserPerTeam <- 用户
我的 Mainteam 表的主键 ID 与表 UserPerTeam 中的 MainTeamID 相关。
我的用户表的主键 ID 与表 UserPerTeam 中的 UserID 相关。
UserPerTeam 有这 2 个关系以及该表中需要的一些其他字段。
因此,现在当我使用以下代码时,我的实体框架会打破它没有主键的事实:
var upt = new FighterPerTeam { UserID = userId, TeamID = teamId };
Entities.UserPerTeam.Attach(upt);
Entities.UserPerTeam.Remove(upt);
Entities.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
所以现在我得到了一个巨大的错误:
无法更新 EntitySet“UserPerTeam”,因为它具有 DefiningQuery,并且元素中不存在支持当前操作的元素。
我只是想知道如何以安全的方式解决这个问题。我知道我可以使用以下代码,但我不太喜欢使用它,因为它看起来不是实体框架。
var cmd = ("Delete from UserPerTeam where userID = " + userId + " and teamId = " + teamId);
Entities.Database.ExecuteSqlCommand(cmd);
Run Code Online (Sandbox Code Playgroud)
差点忘了提及我正在使用延迟加载,并且我只想删除 UserPerTeam 表中的实体。如果您的解决方案还删除了 MainTeam 或 User …
我在实体框架中为两个不同的数据库创建了两个不同的上下文。现在我正在尝试在单个事务中更新这些数据库。我的代码是这样的:
public class LPO_BLL
{
internal Context1 _context1 = null;
internal Context2 _Context2 = null;
public LPO_Detail_BLL(Context1 context1, Context2 context2)
{
_context1 = context1;
_context2 = context2;
}
public void Insert(PM_LPO lpo, LPO_Transaction lpo_transaction)
{
using (TransactionScope transaction = new TransactionScope())
{
_context1.LPO.Add(lpo);
_context1.SaveChanges();
_context2.LPO_Transaction.Add(lpo_transaction);
_context2.SaveChanges(); // I am getting error here...
transaction.Complete();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 UI 项目中,我将其称为:
LPO lpo = new LPO();
//setting properties of lpo
LPO_Transaction lpo_trans = new LPO_Transaction();
//setting properties of lpo_trans
Context1 _context1 = …Run Code Online (Sandbox Code Playgroud) 我的应用程序中有一个DateTime列由 SQLServer 自动计算。创建行时效果很好。不过,我希望以后能够更新。我有标有 的属性[DatabaseGenerated(DatabaseGeneratedOption.Computed)]。
是否可以更新 EF 中的属性?
我有以下型号
public class Parent
{
public int Id { get; set; }
public string Name { get; set; }
public virtual List<Child> Child { get; set; }
}
public class Child
{
public int Id { get; set; }
public string Name { get; set; }
public virtual List<GrandChild> GrandChild { get; set; }
}
public class GrandChild
{
public int Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我现在想做的是从数据库中选择一个包含孩子和孙子的父母列表。
尝试了以下方法,但没有感到高兴:
List<Parent> parent = new …Run Code Online (Sandbox Code Playgroud) entity-framework ×10
c# ×6
.net ×3
ado.net ×1
asp.net ×1
datetime ×1
linq ×1
linq-to-sql ×1
mapping ×1
nullable ×1
sql-server ×1
transactions ×1