ef-code-first

InsertOnSubmit equivalent in DbContext.DbSet using Entity Framework 4 code first

陌路散爱 提交于 2019-12-19 06:08:31
问题 I am using entity framework code first approach, and I am building a generic Repository class that provides data access. In this class I want an Add(T entity) method. However, there is no InsertOnSubmit method as part of the DbSet<T> class, and if I try to use the Add method, I get a compile time error: The type 'TEntity' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Entity.DbContext.Set<TEntity>()' This is the method: public

Linq To Entities - Any VS First VS Exists

你。 提交于 2019-12-19 05:12:28
问题 I am using Entity Framework and I need to check if a product with name = "xyz" exists ... I think I can use Any(), Exists() or First(). Which one is the best option for this kind of situation? Which one has the best performance? Thank You, Miguel 回答1: Any translates into "Exists" at the database level. First translates into Select Top 1 ... Between these, Exists will out perform First because the actual object doesn't need to be fetched, only a Boolean result value. At least you didn't ask

Linq To Entities - Any VS First VS Exists

只愿长相守 提交于 2019-12-19 05:12:02
问题 I am using Entity Framework and I need to check if a product with name = "xyz" exists ... I think I can use Any(), Exists() or First(). Which one is the best option for this kind of situation? Which one has the best performance? Thank You, Miguel 回答1: Any translates into "Exists" at the database level. First translates into Select Top 1 ... Between these, Exists will out perform First because the actual object doesn't need to be fetched, only a Boolean result value. At least you didn't ask

Is it possible to directly reference a many-to-many table using entity framework, code first

隐身守侯 提交于 2019-12-19 05:11:04
问题 I am using the entity framework and modelling a many-to-many relationship. I have created the relationship between the two entities using the fluent API (let's say users and groups): this.HasMany(t => t.Users) .WithMany(t => t.Groups) .Map( m => { m.ToTable("GroupMembers"); m.MapLeftKey("Group_Id"); m.MapRightKey("User_Id"); }); This works great, but I'd like to also be able to reference the GroupMembers table directly. To do that, I have something like: [Table("GroupMembers")] public class

Many to Many relationship in Asp.Net MVC 5 with Identity table and Custom table

此生再无相见时 提交于 2019-12-19 04:15:11
问题 I'm trying to make a relationship between the Users from the table generated by Asp.Net Identity with my own table. The relationship must be many to many, since many Users can work on the same Task (which is my table), and same time an User can work on multiple Tasks. public class Task { public int ID { get; set; } public string Name { get; set; } public string UserID { get; set; } public virtual ICollection<ApplicationUser> Users { get; set; } } public class ApplicationUser : IdentityUser {

Entity Framework + Multilevel Inheritance + EF Code first

拟墨画扇 提交于 2019-12-19 04:09:15
问题 I'm trying to set up a TPC inheritance using Code First. I have a three level heirarchy. Abstract class A, concrete class B inherits from A and a class C inherits from B. Class A properties: ID, CreatedBy and CreatedOn. Class B properties: FirstName, LastName, BirthDate Class C properties: Appointmentdate, Status I want tables for class B and class C to be created in the database with identity generated by the database. I have following code in my context class: public DbSet<B> BList { get;

EF 4.1 RC: Weird Cascade Delete

蹲街弑〆低调 提交于 2019-12-19 03:39:17
问题 I have to admit, the features of EF 4.1 RC Codefirst, DataAnnotations and FluentAPI are still overwhelming to me. Sometimes I really don't know what I am doing ;-) Please see the following POCOs: public class Country { [Key] public Guid ID { get; set; } [Required] public virtual Currency Currency { get; set; } } public class Currency { [Key] public Guid ID { get; set; } public virtual ICollection<Country> Countries { get; set; } } The general idea: Every country needs to have a currency. But

Mocking DbContext.Set<T>()?

你说的曾经没有我的故事 提交于 2019-12-18 20:06:38
问题 We're using EF Code first, and have a data context for our sales database. Additionally, we have a class that sits on top of our data context and does some basic CRUD operations. For example, we have the following function: public static T Create<T>(int userId, T entity) where T : class, IAllowCreate { if (entity == null) throw new ArgumentNullException("entity"); using (SalesContext dc = new SalesContext()) { dc.Set<T>().Add(entity); dc.SaveChanges(); return entity; } } I found an example of

EF 4.1 - Model Relationships

痞子三分冷 提交于 2019-12-18 18:48:11
问题 I'm trying to create a quick ASP.NET MVC 3 application using the RC version of EF 4.1. I have two models: public class Race { public int RaceId { get; set; } public string RaceName { get; set; } public string RaceDescription { get; set; } public DateTime? RaceDate { get; set; } public decimal? Budget { get; set; } public Guid? UserId { get; set; } public int? AddressId { get; set; } public virtual Address Address { get; set; } } and public class Address { public int AddressId { get; set; }

EF Code First: Many-to-many and one-to-many

主宰稳场 提交于 2019-12-18 16:48:48
问题 This is probably just because my knowledge with the EF Code First fluent API is lacking, but I'm stumped. I want to model the following: A Groups collection with Id and Name A Users collection with Id and Name Each user is assigned to exactly one primary group Each user may have zero or many secondary groups The table structure I'm going for would look like: Groups Id Name Users Id Name PrimaryGroupId SecondaryGroupAssignments UserId GroupId I've been beating my head against a wall trying to