entity-framework-4.1

Convert Library from ObjectContext to DbContext

て烟熏妆下的殇ゞ 提交于 2019-12-11 02:04:20
问题 I have a library (based on code found in an old blog post) that allows me to very easily wrap a façade around my data access using Entity Framework. It uses ObjectContext and has performed well enough for my purposes. But now, we are excitedly investigating code first using DbContext and of course would like to reuse / adapt as much of our existing effort as possible. Everything went ok naively converting our Facade enabling library with IObjectContextAdapter until we tried to utilise our

Dynamic Linq failing when using Contains against Int Field

倾然丶 夕夏残阳落幕 提交于 2019-12-11 01:50:46
问题 I'm using Entity Frameworks 4.1.0.0 and MySQL.Data.Entity 6.5.4.0 and when I try and generate a dynamic query for a range of integers, I get an error of: No applicable method 'Contains' exists in type 'Int32' This seems to work fine when using a similar structure to check against Strings..but I want to expand this to support the other db fields I have in my data. Code Example: int[] ids = new int[] { 1, 3, 4 }; IQueryable<entityname> list = db.tablename.Where("Id.Contains(@0)", ids); I have

Have anyone used Entity Framework with code first approach mixed with Edmx file?

牧云@^-^@ 提交于 2019-12-11 01:44:55
问题 I'm currently assign to a project where their legacy system is designed in a horrible way and it's been too much focus on database design. I trying to put together a new design where the customer can migrate the legacy system bit by bit. They are currently using EF 4.1 BUT not code first approach with entity descriptive/mapping is located in an edmx file. They do Reverse engineering everytime to want to extend the model (First make changes in database, then reflect them upwards to Model layer

Concurrency in EF4 - How to conditionally create an entity

眉间皱痕 提交于 2019-12-11 01:36:59
问题 I need to be able to create a new User entity only if the provided email is unique. I've always handled this before by performing a simple if (!UserSet.Any(...)) before my AddToUserSet(...) . However, this is not a concurrent solution and will break under heavy load. I've been looking into Transactions, but AFAIK I would need to set an UPDLOCK on the SELECT too, but EF4 does not support this. How does everyone else handle this? 回答1: You can force locking by including SELECT in transaction:

Entity Framework Code First TPC Inheritance Self-Referencing Child Class

做~自己de王妃 提交于 2019-12-10 23:49:11
问题 I have a problem With Entity Framework and TPC Inheritance With Self-Referencing Child Class. My Class definitions: Public Class BaseObject <Key()> <DatabaseGenerated(DatabaseGeneratedOption.None)> Public Overridable Property iId As Integer <Required()> Public Property iModUserId As Integer <ForeignKey("iModUserId")> Public Property ModUser As User <Required()> Public Property dtModDate As DateTime End Class Public Class User Inherits BaseObject <Required()> Public Property sFirstName As

Are IEnumerable collection supported in EF 4.1 for mapping?

痴心易碎 提交于 2019-12-10 23:22:10
问题 I remember this was a problem with previous version of EF. You couldnt make collections as IEnumerable for exposing your methods instead. This is a problem because i don't want someone to directly access the collection. See this for more info on what i am talking about... Why does the entity framework need an ICollection for lazy loading? So the question is.. are EF 4.1 now support IEnumerable property for mapping relationship ? or is there a solution to this ? Thanks. 回答1: No it doesn't

Entity Framework 4.1. Updating many-to-many relationships. Is this the right way?

萝らか妹 提交于 2019-12-10 23:14:47
问题 The code below works, however, I suspect that I'm missing something. Is there a 'better' way? private void UpdateNew(MarketProduct marketproduct) { context.MarketCategories.Load(); MarketProduct dbProd = context.MarketProducts.Find(marketproduct.Id); dbProd.Categories.Clear(); foreach (var c in marketproduct.Categories ?? Enumerable.Empty<MarketCategory>()) { var cc = context.MarketCategories.Find(c.Id); dbProd.Categories.Add(cc); } context.Entry(dbProd).CurrentValues.SetValues(marketproduct)

Mapping protected properties in Entity Framework 4.1

谁说胖子不能爱 提交于 2019-12-10 22:47:27
问题 I want to have a protected list in a class, accessible via methods. In this topic it's suggested that I could create a configuration class inside my model class, which I don't really like, but as long as it works.. So I have something along those lines: public class Person { public int Id { set; get; } public string Name { set; get; } List<string> _address = new List<string>(); protected virtual ICollection<string> address { get { return this._address; } } [NotMapped] public string[] Address

EF4.1 (code first) - How to specify a composite relationship

北战南征 提交于 2019-12-10 21:34:46
问题 In Linq to SQL I could specify a relationship that didn't have to depend on the foreign keys and pks existing in the database, useful for creating composite relationships like this: public class Equipment_CableNormalised { ... [Association(ThisKey = "EquipmentId,PortNumber", OtherKey = "EquipmentId,PortNumber", IsForeignKey = false)] public List<EquipmentPort> EquipmentPorts { get; set; } } This then generated the sql similar to " .. join EquipmentPorts EP on EP.EquipmentId = blah and EP

Updating record in EF 4.1

六月ゝ 毕业季﹏ 提交于 2019-12-10 20:34:54
问题 I have an employee object say: public class Employee { public int Id {get; set;} public int Name {get; set;} public int Address {get; set;} ...other few 10's of properties } The question is how do I only update Name? For eg. If I want to update Name I do Employee e = Db.Employees.Where(e => e.Id == someId).SingleOrDefault(); e.Name = "Jack"; Db.SaveChanges(); As you can see, for updating I have to first get the object and then update and then call SaveChanges(). For a task that can be done in