entity-framework

Fetching Strategy example in repository pattern with pure POCO Entity framework

北城以北 提交于 2020-01-13 06:52:29
问题 I'm trying to roll out a strategy pattern with entity framework and the repository pattern using a simple example such as User and Post in which a user has many posts. From this answer here, I have the following domain: public interface IUser { public Guid UserId { get; set; } public string UserName { get; set; } public IEnumerable<Post> Posts { get; set; } } Add interfaces to support the roles in which you will use the user. public interface IAddPostsToUser : IUser { public void AddPost(Post

How to create generic EF Insert method?

随声附和 提交于 2020-01-13 06:43:10
问题 I'd like to create a generic C# class with a method that will add a row to a database using Entity Framework. I have one table called Address . I've written the following code to add an address to the database: public class AddressExchange { public int Insert(Address address) { using (var db = new DemoWebEntities()) { //db.AddObject("Address", address); db.Addresses.AddObject(address); db.SaveChanges(); return address.Id; } } } I would like to write a generic class that will perform this

EF5: DB First. Generated Models and Custom Validation

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-13 06:09:46
问题 If I use Code First Development Model , I have a full control of my code. For Example, I have model: User. public class User { [Key] public int id { get; set; } [StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")] [Required(ErrorMessage = "* Login can't be empty")] public string Login { get; set; } [StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")] [Required(ErrorMessage = "* Name can't be empty")] public string FirstName { get; set; }

EF5: DB First. Generated Models and Custom Validation

落花浮王杯 提交于 2020-01-13 06:07:53
问题 If I use Code First Development Model , I have a full control of my code. For Example, I have model: User. public class User { [Key] public int id { get; set; } [StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")] [Required(ErrorMessage = "* Login can't be empty")] public string Login { get; set; } [StringLength(50, ErrorMessage = "* Length might be less then 50 symbols")] [Required(ErrorMessage = "* Name can't be empty")] public string FirstName { get; set; }

Entity Framework taking a lot of time initializing model

邮差的信 提交于 2020-01-13 05:44:32
问题 I'm facing an issue with EF taking a lot of time (spanning hours) to initialize models. The model is as given below. There are around 20 classes which are derived from A1, and about 30 classes which derive from A2. I had to move from TPT to TPH strategy to fix an issue related to memory usage. And since then I'm trying to figure out this issue. The issue is produced by just creating a single instance of type 'A', adding it to a List<A> property of another entity and committing those changes.

LINQ to Entities select columns with expression parameter

帅比萌擦擦* 提交于 2020-01-13 05:18:06
问题 I cannot wrap my head around how i manage to select columns in a queryable by specifying an expression as a parameter. Method A(IQueryable<Order> query) Inside Method A i want to specify which columns to select, so i dont get all columns right away, like this: query.Select(x => new { x.OrderNumber, x.Payment, x.Customer }) This is easy if i specify this directly in Method A, but i want to pass the information using a parameter. I tried using a expression like this: Expression<Func<Order,

Integration testing multiple Entity framework dbcontexts that share a database

◇◆丶佛笑我妖孽 提交于 2020-01-13 05:17:12
问题 In my application I have multiple small entity framework dbcontexts which share the same database, for example: public class Context1 : DbContext { public Context1() : base("DemoDb") { } } public class Context2 : DbContext { public Context2() : base("DemoDb") { } } All database updates are done via scripts and do not rely on migrations (nor will they going forward). The question is - how would you do integration testing against these contexts? I believe there are three options here (there may

Entity Framework - Include in sub query?

落爺英雄遲暮 提交于 2020-01-13 04:29:38
问题 I'm not sure if this has been answered yet, I looked at a couple of questions but I don't think they were quite what I was after. Let's say I have 3 tables: Restaurant 1.....M MenuCategory 1.....M MenuItem I have a L2E query that looks something like this: Restaurant = context.Restaurant .Include(r => r.MenuCategory) .FirstOrDefault(r => r.RestaurantId == resaurantId); Which works to some extent, but it only pre-loads the menu categories. As a work around I am able to iterate around each

Entity Framework - Include in sub query?

*爱你&永不变心* 提交于 2020-01-13 04:29:21
问题 I'm not sure if this has been answered yet, I looked at a couple of questions but I don't think they were quite what I was after. Let's say I have 3 tables: Restaurant 1.....M MenuCategory 1.....M MenuItem I have a L2E query that looks something like this: Restaurant = context.Restaurant .Include(r => r.MenuCategory) .FirstOrDefault(r => r.RestaurantId == resaurantId); Which works to some extent, but it only pre-loads the menu categories. As a work around I am able to iterate around each

AspNet.Identity Custom User and Custom Role should be simple; what am I missing?

偶尔善良 提交于 2020-01-13 03:53:09
问题 Using examples from http://www.asp.net/identity I've gotten this far. The RoleManager works flawlessly and I treat the UserManager the same. I think everything is correct, but I can't seem to new up a UserManager correctly in a controller. What is wrong? At one point, I was successfully getting the UserManager to work but was getting an EntityValidationError saying "Id is required" when creating a new user with UserManager.Create(user, password); as posted in this question UserManager.Create