entity-framework-4.1

How to pass parameters to the DbContext.Database.ExecuteSqlCommand method?

被刻印的时光 ゝ 提交于 2019-11-26 05:16:47
问题 Let\'s just suppose I have a valid need for directly executing a sql command in Entity Framework. I am having trouble figuring out how to use parameters in my sql statement. The following example (not my real example) doesn\'t work. var firstName = \"John\"; var id = 12; var sql = @\"Update [User] SET FirstName = @FirstName WHERE Id = @Id\"; ctx.Database.ExecuteSqlCommand(sql, firstName, id); The ExecuteSqlCommand method doesn\'t allow you to pass in named parameters like in ADO.Net and the

Why is inserting entities in EF 4.1 so slow compared to ObjectContext?

半世苍凉 提交于 2019-11-26 05:14:52
问题 Basically, I insert 35000 objects within one transaction: using(var uow = new MyContext()){ for(int i = 1; i < 35000; i++) { var o = new MyObject()...; uow.MySet.Add(o); } uow.SaveChanges(); } This takes forever! If I use the underlying ObjectContex t (by using IObjectAdapter ), it\'s still slow but takes around 20s. It looks like DbSet<> is doing some linear searches, which takes square amount of time... Anyone else seeing this problem? 回答1: As already indicated by Ladislav in the comment,

Should I enable or disable dynamic proxies with entity framework 4.1 and MVC3?

穿精又带淫゛_ 提交于 2019-11-26 04:37:50
问题 Could someone offer up some advice or point out some blogs/articles that could help with making this decision? The proxies seem very foreign to me and I\'m hesitant to use them. I like the ability to control Lazy Loading by using virtual properties in my model, but that\'s pretty much all the benefits that I can see. My application is a simple MVC web application and I don\'t need to wire up any hooks into the context for when the entities experience a changed state. Anyway, here\'s my very

Entity Framework 4.1 Virtual Properties

给你一囗甜甜゛ 提交于 2019-11-26 04:36:04
问题 If i have declared entity relationship in my model as virtual then there is no need to use the Include statement in my LINQ query, right ??- For ex: This is my model class : public class Brand { public int BrandID { get; set; } public string BrandName { get; set; } public string BrandDesc { get; set; } public string BrandUrl { get; set; } public virtual ICollection<Product> Products { get; set; } } Now, for the above model class, i dont need to use the var brandsAndProduct = pe.Brands.Include

Entity Framework code first. Find primary key

天大地大妈咪最大 提交于 2019-11-26 04:25:18
问题 How do I find which property of a class is the primary key of the Entity Framework Code First entity POCO? Please note string matching for Id / class name + \"Id\" is a bad option. There must be some way to dig out the convention used by Entity Framework and reliably getting the key property. Thanks in advance. 回答1: You can ask mapping metadata to get names of key properties (there can be more then one): ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext; ObjectSet

Ignoring a class property in Entity Framework 4.1 Code First

本小妞迷上赌 提交于 2019-11-26 03:42:59
My understanding is that the [NotMapped] attribute is not available until EF 5 which is currently in CTP so we cannot use it in production. How can I mark properties in EF 4.1 to be ignored? UPDATE: I noticed something else strange. I got the [NotMapped] attribute to work but for some reason, EF 4.1 still creates a column named Disposed in the database even though the public bool Disposed { get; private set; } is marked with [NotMapped] . The class implements IDisposeable of course but I don't see how that should matter. Any thoughts? You can use the NotMapped attribute data annotation to

How Should I Declare Foreign Key Relationships Using Code First Entity Framework (4.1) in MVC3?

放肆的年华 提交于 2019-11-26 02:41:24
问题 I have been searching for resources on how to declare foreign key relationships and other constraints using code first EF 4.1 without much luck. Basically I am building the data model in code and using MVC3 to query that model. Everything works via MVC which is great (kudos to Microsoft!) but now I want it NOT to work because I need to have data model constraints. For example, I have a Order object that has a ton of properties that are external objects (tables). Right now I can create an

How to filter nested collection Entity Framework objects?

余生长醉 提交于 2019-11-26 02:14:15
问题 Here is the problem: I need to return a collection of objects with filtered nested collections. E.g: there is a store with orders and I need to return a collection with stores that includes nested collections with orders but without orders from customers that are marked as deleted. Here is what I try to do. But still no luck. Any suggestions are appreciated :) public List<StoreEntity> GetStores(Func<Store, bool> storeFilter, Predicate<OrderEntity> orderFileter) { IQueryable<StoreEntity>

Is DbContext thread safe?

▼魔方 西西 提交于 2019-11-26 01:54:19
问题 I was wondering if the DbContext class is thread safe, I am assuming it\'s not, as I am currently executing paralell threads that access the DbContext in my application and I am getting a host of locking exceptions and other things that look like they may be thread related. Until recently I wasn\'t getting any errors...but until recently I wasn\'t accessing the DbContext in the threads. If I am right, what would people suggest as a solution? 回答1: It's not thread safe. Simply create a new

Unit Testing DbContext

谁说我不能喝 提交于 2019-11-26 01:49:35
问题 I\'ve researched some information about techniques I could use to unit test a DbContext . I would like to add some in-memory data to the context so that my tests could run against it. I\'m using Database-First approach. The two articles I\'ve found most usefull were this and this. That approach relies on creating an IContext interface that both MyContext and FakeContext will implement, allowing to Mock the context. However, I\'m trying to avoid using repositories to abstract EF, as pointed by