entity-framework-4.1

Entity Framework: Querying Child Entities

筅森魡賤 提交于 2019-11-26 15:32:01
问题 I'm learning about Entity Framework at the mo, and am having problems!! Can someone clarify if I am right in thinking that I can't get a parent and a subset of it's children from the db? For example... db.Parents .Include(p => p.Children) .Where(p => p.Children.Any(c => c.Age >= 5)) This will return all Parents that have a child aged 5+, but if I iterate through the Parents.Children collection, all children will be present (not just those over 5 years old). Now the query does make sense to me

Entity Framework code first. Find primary key

孤街浪徒 提交于 2019-11-26 15:23:56
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. You can ask mapping metadata to get names of key properties (there can be more then one): ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext; ObjectSet<YourEntity> set = objectContext.CreateObjectSet<YourEntity>(); IEnumerable<string> keyNames = set.EntitySet

Create association on non-primary key fields with Entity Framework 4.1 Fluent API

╄→尐↘猪︶ㄣ 提交于 2019-11-26 14:24:23
问题 We are using EF 4.1 and the fluent API to get data from a legacy database (that we are not permitted to change). We are having a problem creating a relationship between two tables where the related columns are not primary and foreign keys. With the classes below, how would we configure the one-to-many relationship between Report and RunStat such that Report.RunStats would return all of the RunStat entities where the ReportCode fields are equal? public class Report { [Key] public int ReportKey

Why is .Contains slow? Most efficient way to get multiple entities by primary key?

 ̄綄美尐妖づ 提交于 2019-11-26 14:13:23
What's the most efficient way to select multiple entities by primary key? public IEnumerable<Models.Image> GetImagesById(IEnumerable<int> ids) { //return ids.Select(id => Images.Find(id)); //is this cool? return Images.Where( im => ids.Contains(im.Id)); //is this better, worse or the same? //is there a (better) third way? } I realise that I could do some performance tests to compare, but I am wondering if there is in fact a better way than both, and am looking for some enlightenment on what the difference between these two queries is, if any, once they have been 'translated'. Slauma UPDATE:

Set database collation in Entity Framework Code-First Initializer

浪子不回头ぞ 提交于 2019-11-26 14:07:21
问题 I want to set the default collation for a database, when Entity Framework Code First creates it. I've tried the following: public class TestInitializer<T> : DropCreateDatabaseAlways<T> where T: DbContext { protected override void Seed(T context) { context.Database.ExecuteSqlCommand("ALTER DATABASE [Test] SET SINGLE_USER WITH ROLLBACK IMMEDIATE"); context.Database.ExecuteSqlCommand("ALTER DATABASE [Test] COLLATE Latin1_General_CI_AS"); context.Database.ExecuteSqlCommand("ALTER DATABASE [Test]

EF 4.1 loading filtered child collections not working for many-to-many

给你一囗甜甜゛ 提交于 2019-11-26 14:01:26
问题 I've been looking at Applying filters when explicitly loading related entities and could not get it to work for a many-to-many relationship. I created a simple model: Brief description: A Student can take many Courses and a Course can have many Students . A Student can make many Presentation , but a Presentation can be made by only one Student . So what we have is a many-to-many relationship between Students and Courses , as well as a one-to-many relationship between Student and Presentations

Entity Framework 4.1 default eager loading

女生的网名这么多〃 提交于 2019-11-26 12:44:29
问题 I\'m using Entity Framework 4.1 code first approach. I want to make eager loading as my the dafault configuration, and by that avoid using the Include extension method in each fetching query. I did as recomended in MSDN, changing the simple lazy property at the DbContext constructor: public class EMarketContext : DbContext { public EMarketContext() { // Change the default lazy loading to eager loading this.Configuration.LazyLoadingEnabled = false; } } unfortunately, this approach is not

Entity 4.1 Updating an existing parent entity with new child Entities

前提是你 提交于 2019-11-26 12:11:34
问题 I have an application where you can create a new type of product and add to that product some ingredients. The product and the ingredients are both entities saved in a database. The product entity has a collection of ingredient entities. (simplified version) public class Product Public Sub New() Me.Ingredients = New List(Of Ingredient)() End Sub Property Ingredients as ICollection(Of Ingredient) end class When I save the product for the first time, all goes well: I just add it to the context

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

牧云@^-^@ 提交于 2019-11-26 12:03:45
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 Order no problem, but without being able to add the foreign key or external objects. MVC3 sets this up no

EF Code First DBContext and Transactions

谁说胖子不能爱 提交于 2019-11-26 11:51:42
问题 I would like know what is the best possible way to implement transactions with DBContext . In particular, Does DbContext.SaveChanges implement transaction internall if i change multiple entities? If i want to call DbContext.SaveChanges multiple times(same contxet/different contxets), how transaction can be achieved? 回答1: Yes. SaveChanges uses transaction internally. Use TransactionScope to wrap multiple calls to SaveChanges Example: using(var scope = new TransactionScope