entity-framework-4.1

Can a DbContext enforce a filter policy?

≡放荡痞女 提交于 2019-11-26 09:20:08
问题 I would like to pass a value to the ctor of a DbContext and then have that value enforce \"filtering\" on the related DbSets. Is this possible...or is there a better approach? Code might look like this: class Contact { int ContactId { get; set; } int CompanyId { get; set; } string Name { get; set; } } class ContactContext : DbContext { public ContactContext(int companyId) {...} public DbSet<Contact> Contacts { get; set; } } using (var cc = new ContactContext(123)) { // Would only return

Order navigation properties when using Include and/or Select methods with EF 4.1 Code-First?

北战南征 提交于 2019-11-26 08:19:33
问题 This is the second step of a question explained here: EF 4.1 code-first: How to load related data (parent-child-grandchild)?. With @Slauma\'s guidance, I have successfully retrieved data with this approach: var model = DbContext.SitePages .Where(p => p.ParentId == null && p.Level == 1) .OrderBy(p => p.Order) // ordering parent .ToList(); foreach (var child in model) { // loading children DbContext.Entry(child) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering children

Generic Way to Check If Entity Exists In Entity Framework?

拟墨画扇 提交于 2019-11-26 08:05:29
问题 Similar to Best way to check if object exists in Entity Framework? I\'m looking for a generic way to check for an entity in a DbSet . Something like this, which doesn\'t work: private DbContext DbContext { get; set; } private DbSet<T> DbSet { get; set; } public Boolean Exists(T entity) { return ((from item in this.DbSet where item == entity select item).Count() > 0); } The line where item == entity works in LINQ to SQL, but apparently not with LINQ to Entities. Since the entities may have

EF Code First - how to set identity seed?

社会主义新天地 提交于 2019-11-26 07:46:27
问题 I have a entity class public class Employee { public long Id { get; set; } public string Name { get; set; } } I have set the Id field as the primary key with auto number generation modelBuilder.Entity<Employee>().HasKey(e => e.Id); modelBuilder.Entity<Employee>().Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); But I want the Identity to seed from 10000 instead of from 1 which is the default. How can I specify this in EF? 回答1: If you are using SQL Server you

EF Code First: How to get random rows

假如想象 提交于 2019-11-26 07:34:47
问题 How can I build a query where I would retrieve random rows? If I were to write it in SQL then I would put an order by on newid() and chop off n number of rows from the top. Anyway to do this in EF code first? I have tried creating a query that uses newid() and executing it using DbSet.SqlQuery(). while it works, its not the cleanest of solutions. Also, tried retrieve all the rows and sorting them by a new guid. Although the number of rows are fairly small, its still not a good solution. Any

Is UnitOfWork and GenericRepository Pattern redundant In EF 4.1 code first?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 07:29:36
问题 Wondering if I need to use the Genericrepository pattern and UnitOfWork to mock the repository.I am using MOQ.Is it now redundant since I have noticed that EF 4.1 has IDBSet. I have not figured out how to write something generic that usic IDBSet .If you have an example where you implement IDBSet can you show it to me? Any suggestions? 回答1: This is duplicate of many topics already discussed on SO but I agree that some of them can be hard to find because they are nested in other question What's

How to include a child object&#39;s child object in Entity Framework 5

谁都会走 提交于 2019-11-26 07:25:40
问题 I am using Entity Framework 5 code first and ASP.NET MVC 3 . I am struggling to get a child object\'s child object to populate. Below are my classes.. Application class; public class Application { // Partial list of properties public virtual ICollection<Child> Children { get; set; } } Child class: public class Child { // Partial list of properties public int ChildRelationshipTypeId { get; set; } public virtual ChildRelationshipType ChildRelationshipType { get; set; } } ChildRelationshipType

Overriding SaveChanges and setting ModifiedDate, but how do I set ModifiedBy?

狂风中的少年 提交于 2019-11-26 06:37:22
问题 I have an ASP.NET MVC3 web application with UI, Business (entities), and Data (DbContext) layers. I am using Entity Framework 4.1 Code First. Right now, I am overriding the DbContext.SaveChanges() in the Data layer so that I can set the ModifiedDate for all changes made to any entity objects that implement my IAuditable interface. I have a static DateProvider class and method (GetCurrentDate) that returns DateTime.Now (unless I\'m running a test, in which case, it returns whatever I told it

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

丶灬走出姿态 提交于 2019-11-26 05:56:03
问题 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

Is DbContext thread safe?

淺唱寂寞╮ 提交于 2019-11-26 05:17:36
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? It's not thread safe. Simply create a new instance of DbContext in you thread. No it is not thread safe - whole EF is not thread safe because EF context