entity-framework-4

How to avoid polymorphic behaviour in Entity Framework TPT Inheritance in order to query base type efficiently

吃可爱长大的小学妹 提交于 2019-12-05 15:24:35
Overview I am using entity framework 4.3 code first with the fluent interface to setup my DbContext. I have a base Item class with other types that inherit this such as an Event , BlogPost , ForumThread , WikiPage and so on. These inherited types are mapped with what I think entity framework refers to as TPT inheritance. This works great when querying a single type like 'events' or 'blog posts' but constructs very complicated queries with horrible performance when trying to query across all types due to the joins required in order to achieve the polymorphic behaviour EF provides out of the box

Entity Framework - pessimistic locking

微笑、不失礼 提交于 2019-12-05 15:16:01
What I am trying to do is basically what NHibernate does when you do something like: var instance = session.Get<Customer>(id, LockMode.Upgrade); I need to lock the entity row in the database. Why I need that? Imagine something like a workflow instance that can be updated by multiple actors(people or processes) at the same time. The constraints I have don't allow for optimistic locking solutions. EF doesn't have support for this. If you want to lock some record by query you must do something like: using (var scope = new TransactionScope(...)) { using (var context = new YourContext(...)) { var

Entity Framework 4 Abstract Model - How to Programatically Eager-Load Navigational Properties?

自闭症网瘾萝莉.ら 提交于 2019-12-05 15:13:44
I have an EF4 Model that is built with abstract entities/classes: Notice how State entity has a navigational property called Country . Note: I have lazy-loading disabled, so i must eager-load on demand. Now, if i have the following method: public Location FindSingle(int id) { return _repository.Find().WithId(id).SingleOrDefault(); } This does not return any associations by default. But how can i dynamically eager-load the associations when i explicitly want to? I cannot do this: return _repository.Find().WithId(id).Include("Country").SingleOrDefault(); As i am working with an abstract class

Simplest Way to Delete Object with Entity Framework 4

孤街醉人 提交于 2019-12-05 15:04:34
Ack! I'm new to Entity Framework and am trying to find the simplest way to delete an item. I have a listbox with the datasource set to TagCategory objects from the database. This is working fine. Now I'd like to delete the selected item. So I do something like this: TagCategory category = (TagCategory)lstCategories.SelectedItem; using (MyEntities context = new MyEntities()) { context.AttachTo("TagCategories", category); context.DeleteObject(category); context.SaveChanges(); } This seems straight forward enough, but it doesn't work. Nothing is deleted, no error message, nothing. So I see I can

Entity Framework 4 very slow linking an Order to Customer who has 10,000 orders

点点圈 提交于 2019-12-05 14:37:44
This one has me stumped. I have a Customer and Order entity. the Customer can have many Orders. Where the Customer has 10,000 orders when I create a new Order and set the Customer property (Order.Customer = customer) there is a LONG delay (20s of seconds). It appears that the context is loading all 10,000 orders before adding this new one. I am not currently using FKs directly which I suspect may help. Any ideas how I can improve matters without a massive refactor? Cheers. The problem is most probably that you are using T4 POCO template. This template generates nasty fixup methods and use them

C# How to use DataAnnotations StringLength and SubString to remove text

坚强是说给别人听的谎言 提交于 2019-12-05 13:52:11
问题 I have a model classes that has a description property with a data annotation attribute of StringLength and length is set to 100 characters. When this property is more than 100 characters and Entity Framework tries to save this property I get the following error. [StringLength(100, ErrorMessage = "Description Max Length is 100")] public string Description { get; set; } Error: "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details" I'm not sure if

An exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

时间秒杀一切 提交于 2019-12-05 13:08:16
I am new to ASP.NET MVC, I am facing this exception, the connection string looks perfect but still, the exception is raised, appreciate if anyone give me why is happening. Thank you guys Model 1 namespace MVCTwice.Models { public class StudentContext : DbContext { public DbSet<Student> studs { get; set; } } } Model 2 namespace MVCTwice.Models { [Table("tblStudents")] public class Student { public int id { get; set; } public string name { get; set; } public string gender { get; set; } public string totalMarks { get; set; } } } Action method public ActionResult Index() { StudentContext

Entity Framework: Alternate solution to using non primary unique keys in an association

妖精的绣舞 提交于 2019-12-05 12:45:52
问题 I know the entity frame work does not allow you to generate a model from a database using non primary unique keys as a Foreign Key association. Can I modify the EDMX manually? If so, can someone provide me an example or reference? If not, are there any other possibilities? Easiest Example: Here is the DDL for the tables. You will notice I have a foreign Key from PersonType.TypeCode to Person.TypeCode CREATE TABLE [dbo].[PersonType]( [PersonTypeId] [int] NOT NULL, [TypeCode] [varchar](10) NOT

What is the purpose of .edmx files?

不羁的心 提交于 2019-12-05 11:57:35
问题 What is the purpose of .edmx files? Reading the CSDL, SSDL, and MSL specifications, it looks to me like .edmx files are used only at design time. Are we meant to distribute it with the other edmx? It seems like we need to distribute the .ssdl and/or .csdl files instead. 回答1: EDMX is Visual Studio's "container" for all things about your Entity Data Model. It contains all the information that is in the CSDL, SSDL, MSL, plus information about the visual layout of the tables in your Visual Studio

Entity Framework 4 Code Only Error “Multiple objects sets per type are not supported”

微笑、不失礼 提交于 2019-12-05 10:50:57
问题 I have two "Code Only" POCO's using EF4 and the latest CTP, running against an existing, legacy database. Running a LINQ query against PocoA worked until I added the property below to that object, I was trying to add a relationship. public virtual PocoB pocoB { get; set; } Once I did that, I started getting the following error: Multiple object sets per type are not supported. The object sets 'PocoA_DbSet' and 'PocoB_DbSet' can both contain instances of type 'PocoA'. So I next thought my