据我所知,Entity Framework实现了Identity Map Pattern,因此EF会在内存中缓存一些实体.
我举个例子吧.
var context = new StudentContext();
var student = context.Students.Where(st => st.Id == 34).FirstOrDefault();
// any way of changing student in DB
var anotherContext = new StudentContext();
var anotherStudent = anotherContext.Students.Where(st => st.Id == 34).FirstOrDefault();
anotherStudent.Name = "John Smith";
anotherContext.SaveChanges();
student = context.Students.Where(st => st.Id == 34).FirstOrDefault();
// student.Name contains old value
Run Code Online (Sandbox Code Playgroud)
有没有办法使第一个上下文的缓存无效并检索新student实体而无需重新创建上下文?
感谢帮助.
.net caching entity-framework identity-map entity-framework-4
我猜这是不可能的,但无论如何我会把它扔出去.在CTP4中使用EF4 CodeFirst API进行编程时是否可以使用CreateSourceQuery?我想热切地加载附加到属性集合的属性,如下所示:
var sourceQuery = this.CurrentInvoice.PropertyInvoices.CreateSourceQuery();
sourceQuery.Include("Property").ToList();
Run Code Online (Sandbox Code Playgroud)
但是当然定义了EntityCollection<T>CreateSourceQuery,而CodeFirst使用普通的ICollection(显然).有什么方法可以转换吗?
我已经得到了以下工作,但这并不是我想要的.任何人都知道如何从下面的内容到上面的内容(下面的代码来自一个继承DbContext的类)?
ObjectSet<Person> OSPeople = base.ObjectContext.CreateObjectSet<Person>();
OSPeople.Include(Pinner => Pinner.Books).ToList();
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑:这是我的版本由zeeshanhirani发布的解决方案 - 顺便说一下,这本书是惊人的!
dynamic result;
if (invoice.PropertyInvoices is EntityCollection<PropertyInvoice>)
result = (invoices.PropertyInvoices as EntityCollection<PropertyInvoice>).CreateSourceQuery().Yadda.Yadda.Yadda
else
//must be a unit test!
result = invoices.PropertyInvoices;
return result.ToList();
Run Code Online (Sandbox Code Playgroud)
EDIT2:
好吧,我刚刚意识到你在使用动态时无法调度扩展方法.所以我想我们是不是很为Ruby的动态,而上面的例子是易于修改这一限制相称
EDIT3:
正如zeeshanhirani的博客文章中所提到的,只有当(并且仅当)您具有启用更改的代理时才会起作用,如果您的所有属性都被声明为虚拟,则会创建代理.这是使用带有POCO的CreateSourceQuery的方法的另一个版本
public class Person {
public virtual int ID { get; set; }
public virtual string FName { get; set; }
public virtual string LName { …Run Code Online (Sandbox Code Playgroud) 给定MS SQL 2008中的"用户"表和"登录"表:
CREATE TABLE [dbo].[User_User](
[UserID] [int] IDENTITY(1000,1) NOT NULL,
[UserName] [varchar](63) NOT NULL,
[UserPassword] [varchar](63) NOT NULL
)
CREATE TABLE [dbo].[Util_Login](
[LoginID] [int] IDENTITY(1000,1) NOT NULL,
[User_UserID] [int] NOT NULL, -- FK REFERENCES [dbo].[User_User] ([UserID])
[LoginDate] [datetime] NOT NULL,
)
Run Code Online (Sandbox Code Playgroud)
如何调整User_User实体框架模型对象以包含返回MAX(LoginDate)的"UserLastLogin"列?
我知道我可以围绕SQL视图创建一个EF4模型:
CREATE VIEW [v_User_User]
AS
SELECT
[User_User].*,
(
SELECT MAX(LoginDate)
FROM [Util_Login]
WHERE User_UserID = UserID
) AS UserLastLogin
FROM [User_User]
Run Code Online (Sandbox Code Playgroud)
但有没有办法可以修改User_User模型以包含计算的列?
编辑:我正在寻找一种方法来获取用户或列表<用户>包括单个数据库查询中的最大(Util.LastLogin)日期.
sql-server entity-framework calculated-columns entity-framework-4
我刚刚从Linq 2 SQL切换到Entity Framework,我在EF中看到了一些奇怪的行为,我希望有人可以提供帮助.我尝试使用谷歌搜索,但我无法找到同样问题的其他人.我嘲笑了一个场景来解释这种情况.
如果我直接使用EF上下文,我可以在select中进行选择.例如,这执行得非常好:
// this is an Entity Framework context that inherits from ObjectContext
var dc = new MyContext();
var companies1 = (from c in dc.Companies
select new {
Company = c,
UserCount = (from u in dc.CompanyUsers
where u.CompanyId == c.Id
select u).Count()
}).ToList();
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用存储库返回IQueryable(甚至是ObjectSet或ObjectQuery)的存储库模式,我会得到一个NotSupportedException(LINQ to Entities无法识别方法'System.Linq.IQueryable`1)...
这是我的存储库的一个例子:
public class Repository {
private MyContext _dc;
public Repository() {
_dc = new MyContext();
}
public IQueryable<Company> GetCompanies() {
return _dc.Companies;
}
public IQueryable<CompanyUser> GetCompanyUsers() {
return _dc.CompanyUsers;
} …Run Code Online (Sandbox Code Playgroud) 我是EF的亲戚,并使用EF4创建了一个简单的模型(见下文).

我在将新记录插入UserRecord实体并随后添加使用新创建的UserRecordId作为其主键的UserDetail实体时遇到问题.下面显示的代码用于在我的数据库具有一对多关系时工作,但当我将其更改为一对一关系时,我会在下面的图像中突出显示错误.

我认为它不起作用,因为现在没有与UserRecord关联的UserDetails属性,因为它现在是一对一的关系.我的问题是我现在如何以一对一的关系插入新的UserRecord和相应的UserDetail实体?
任何有关这一方面的帮助都非常受欢迎,因为一直在搜索网络并尝试各种不同的事情而没有成功.
干杯
Cragly
我从数据库生成了一个.edmx文件.
我想知道是否对数据库模式进行了更改,那么这些更改将如何反映在我的实体数据模型设计器图中?我对我的数据库模式进行了更改,但发现更改没有出现在我的实体数据模型设计器图中.有人可以向我解释如何将数据库架构更改传播到我的实体数据模型设计器图表吗?
我需要绑定GridView,我正在使用此代码:
ProductDBEntities db = new ProductPDBEntities();
var pro = from u in db.Products where u.PID == 1 select u;
if (pro != null)
{
GridView1.DataSource = pro;
GridView1.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
并得到这个错误.
System.InvalidOperationException:Sequence包含多个元素
有人可以告诉我,我做错了什么?
使用MVC3更新对象
我有一个可以修改的模型,请看下面的示例:
[HttpPost]
public ActionResult Edit(Company c)
{
if (ModelState.IsValid)
{
db.Entry(c).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(c);
}
Run Code Online (Sandbox Code Playgroud)
该模型具有未在视图中显示的其他字段,并且无法由用户修改,但是当我单击提交按钮时,未在视图中显示的字段设置为null.
我可以以某种方式让EF知道不修改某些字段吗?谢谢.
我有实体框架设置来更新表.更新被一个代替触发器拦截,该触发器调用RAISERROR:
CREATE TRIGGER mySchema.UpdateBusinessObjects
ON mySchema.BusinessObjects
INSTEAD OF UPDATE
AS
RAISERROR(''test error from SQL'',16,1)
RETURN
Run Code Online (Sandbox Code Playgroud)
在我的repository类中,我试图捕获SQL中RAISERROR生成的SqlException:
public void SaveBusinessObject(BusinessObject b) {
try {
repo.Entry(b).State = EntityState.Modified;
repo.SaveChanges();
} catch (SqlException ex) {
// handle exception here
}
}
Run Code Online (Sandbox Code Playgroud)
问题是C#没有捕获SqlException; 它作为未处理的异常传递给调用者("用户未处理SqlException:来自SQL的测试错误").什么?!
看起来EF的SaveChanges()不知何故在我的try catch块上传递异常.我已经尝试将我的catch语句切换为catch(Exception ex),以防EF异常在某种程度上更通用,但我仍然得到一个未处理的SqlException.我错过了一些简单的东西吗?SaveChanges()方法有什么问题?
我正在尝试更新表中的单个记录,但是当我运行时.Firstordefault(),我得到错误:"对象引用未设置为对象的实例.",如果使用它.First(),我得到"序列不包含任何元素".
使用它的另一个地方,它的工作正常,但这次它导致错误.
这是代码:
public class AllownceDetails
{
public int ta_id{get;set;}
public int tvrid{get;set;}
public DateTime ofDate{get;set;}
public string status{get;set;}
public string userid {get;set;}
}
//Update Method
public void Update(AllownceDetails Allowncedtl)
{
var ta = (from a in ce.tbl_tvrallownce
where a.tvrid == Allowncedtl.tvrid
//error: Sequence contains no elements
select a).SingleOrDefault();
ta.status = Allowncedtl.status;
//error:Object reference not set to an instance of an object
ce.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud) c# ×5
.net ×2
asp.net ×2
linq ×2
c#-4.0 ×1
caching ×1
identity-map ×1
iqueryable ×1
sql-server ×1
triggers ×1