entity-framework-4.1

Entity Framework 4.1 Code First: Get all Entities with a specific base class

拜拜、爱过 提交于 2020-01-01 05:13:12
问题 I have a DbContext with set up different DbSet<T> s with all types that derive from the same base class: public class Foo : Entity { } public class Bar : Entity { } MyDbContext : DbContext { public DbSet<Foo> Foos { get; set; } public DbSet<Bar> Bars { get; set; } } Is it possible to get all entities which have the Entity base class in one query, like: DbContext.Set<Entity>(); // doesn't work I tried to introduce an explicit DbSet<Entity> to the DbContext, but that results in one big table

Stored Procedures and EF Code First

狂风中的少年 提交于 2020-01-01 04:39:06
问题 I would like to use a stored procedure to retrieve entities from a DB, I don't care about tracking changes. I just need all entities be resolved including related ones. Do I have to use SqlCommand? What about complex properties, will they be resolved too? Any other limitations you could tell me about? Thanks! 回答1: General answer about using stored procedures in EF is here so stored procedure in pure EF will not handle navigation properties. The answer also mentioned EFExtensions but that is

EF Code First thinks database is out of date — only in production

和自甴很熟 提交于 2020-01-01 03:42:05
问题 I have an ASP.NET MVC 3 app that is using SQL Server CE 4.0 and Entity Framework 4.1 Code First. It works fine locally , but in production, it fails with the following error: The model backing the 'foobar' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and

Why does Entity Framework Code-First (with an existing DB) keep trying get data from an EdmMetadata table?

给你一囗甜甜゛ 提交于 2020-01-01 03:04:06
问题 i'm trying do some Entity Framework Code First programming to an existing database .. but I keep seeing this code in my Sql Profiler :- SELECT TOP ( 1 ) [Extent1].[Id] AS [Id], [Extent1].[ModelHash] AS [ModelHash] FROM [dbo].[EdmMetadata] AS [Extent1] ORDER BY [Extent1].[Id] DESC What the hell is this EdmMetadata table and why do is my EF code trying to grab the Id and ModelHash from there? Remember, I'm trying to work against an existing DB. Cheers :) 回答1: There is no Code-First against

Partially updating object with EF Code First and ASP.NET MVC

这一生的挚爱 提交于 2020-01-01 00:21:13
问题 EF4.1-Code-First-Gurus! I wonder if there is a more elegant way to handle the following ASP.NET MVC 3 EF 4.1 Code First scenario: Lets say we have the following POCOs: public class Entity { [Key] public int Id { get; set; } [ScaffoldColumn(false)] public DateTime CreatedOn { get; set; } [ScaffoldColumn(false)] public DateTime ModifiedOn { get; set; } } and public class Person : Entity { public string FirstName { get; set; } public string LastName { get; set; } public DateTime Birthday { get;

Get a list of elements by their ID in entity framework

陌路散爱 提交于 2019-12-31 17:51:12
问题 How can I get all elements that are in another list by ID? For eg; I have List roles; I'd like to get all roles from the database that are in this this list by their Id. I'm using code-first. I did this and it threw an error: var roles = db.Roles.Where(r => user.Roles.Any(ur => ur.RoleId == r.RoleId)); RoleId is of type int. Error: Unable to create a constant value of type 'SampleMVC.Domain.Role'. Only primitive types ('such as Int32, String, and Guid') are supported in this context. 回答1: var

Code first - Cannot insert the value NULL into column 'Id'

萝らか妹 提交于 2019-12-31 17:50:20
问题 This is my code, very simple... var newUser = new User(); newUser.Id=id; newUser.Email = email; this.DataContext.Set<User>().Add(newUser); this.DataContext.SaveChanges(); The error I get is a sqlexception at this.DataContext.SaveChanges(); stating that: Cannot insert the value NULL into column 'Id', table 'xxxxx.dbo.Users'; column does not allow nulls. INSERT fails. I have debugged and found that there is value in Id & Email in newUser at this.DataContext.Set<User>().Add(newUser); If this is

Entity Framework Code First - Cannot insert duplicate key in object 'db'

余生长醉 提交于 2019-12-31 04:53:07
问题 I'm using the new EF code first to one project of mine, and i'm getting a weird error, my mode is: abstract class Member { public virtual int MemberId; ... some other stuff } class User : Member { public virtual string Name; ... some more props no new key defined } class MyDbContext { public DbSet<User> Users {get;set;} } The result of the deployment to SQL Server is just ok, i've a Members Table and a Users Table (TPC). Now in my main i've the following code: static Main() { User x,y; using

Conditional Validation on MVC3 Model Class

不羁岁月 提交于 2019-12-31 03:45:13
问题 I'm using Entity Framework and a Model class, DonationForm, wrapped by a view model class "CreateDonationForm". In keeping with the DRY principle, I have added the validation annotations on the Model class (not the just the view model), so that they will be reusable. However, not all of the properties on the class will always be used (some are in fact mutually exclusive). For example when a particular phone number property is relevant, I want to make it conform to a Regex annotation and be

Entity Framework Code First IQueryable

和自甴很熟 提交于 2019-12-30 17:31:46
问题 I am using Entity Framework Code First and ran into a small road block. I have a class "Person" defined as such: public class Person { public Guid Id { get; set; } public virtual ICollection<History> History { get; set; } } and a class "History" defined as such: public class History { public Guid Id { get; set; } public virtual Person Owner { get; set; } public DateTime OnDate { get; set; } } However, when I call: IEnumerable<History> results = person.History .OrderBy(h => h.OnDate) .Take(50)