code-first

How to brings the Entity Framework Include extention method to a Generic IQueryable<TSource>

戏子无情 提交于 2019-12-06 03:08:28
问题 Here's the thing. I have an interface, and I would to put the Include extension method, who belongs to EntityFramework library, to my IRepository layer wich dont needs to knows about EntityFramework . public interface IRepository<TEntity> { IQueryable<TEntity> Entities { get; } TEntity GetById(long id); TEntity Insert(TEntity entity); void Update(TEntity entity); void Delete(TEntity entity); void Delete(long id); } So I have the extension method: public static class IncludeExtension { static

Entity Framework CTP5 Code-First: Mapping a class with multiple collections of another class

99封情书 提交于 2019-12-06 02:31:21
问题 With EF CTP5 Code-First I am trying to map a class model which contains multiple collections in one class pointing to another class. Here is an example of what I mean: public class Company { public int CompanyId { get; set; } public IList<Person> FemaleEmployees { get; set; } public IList<Person> MaleEmployees { get; set; } } public class Person { public int PersonId { get; set; } public Company Company { get; set; } } If I let the database create from this model with a DbContext without

EF Migrations error: could not load type 'System.Data.Entity.Infrastructure.DbContextInfo'

允我心安 提交于 2019-12-05 22:23:57
I am using ContosoUniversity example. I have just used Nuget to download and install code first migrations pakage. Whe I excecute update-database command it throws an error . Is there anything to do more than installing nuget package? Update-Database : Could not load type 'System.Data.Entity.Infrastructure.DbContextInfo' from assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. At line:1 char:16 + update-database <<<< + CategoryInfo : NotSpecified: (:) [Update-Database], TypeLoadException + FullyQualifiedErrorId : System.TypeLoadException,System.Data

One out of 2 properties should be null (EntityFramework Code First)

China☆狼群 提交于 2019-12-05 18:45:26
I've searched a lot in the forum, but haven't found anything about regarding this issue. I have 2 properties in the EntityFramework Code First: [Column(TypeName = "Money")] public decimal? Debit { get; set; } [Column(TypeName = "Money")] public decimal? Credit { get; set; } One of them should be not null, but the other one should be null Examples: Debit=null; Credit=34; Debit=45; Credit=null; On the other hand, it should not be possible to set both or none of them null. Is it possible to handle this issue with data annotations or should I solve it with a workaround? Best regards! You could

Entity Framework Code First Data Migrations not working with VS2012 Web Deploy

旧巷老猫 提交于 2019-12-05 18:43:16
问题 I have created an MVC 3.0 application using Visual Studio 2012, .NET 4.5 and Entity Framework 5.0. Using Code First Data Migrations, I am able to correctly propagate model changes to my local test database, but I can't figure out how to get this to work when deploying to my staging and production servers using Web Deploy. I have read the following article ... http://msdn.microsoft.com/en-us/library/dd394698(v=vs.110)#dbdacfx ... which explains what's supposed to happen, but it's not working

How do I represent an option calculated column with EF Code First?

纵饮孤独 提交于 2019-12-05 18:22:45
I have a situation where I have a table of entries that I'll be querying on, but in some cases I'll have additional information available. For instance, if I have a table of people , I want to be able to search by name, but I also want to store some coordinates and search based on their location, but also expose that distance in the object model. So, for instance, suppose my people table looks something like this: PersonId int Name nvarchar(100) Address nvarchar(100) Latitude float(10,6) Longitude float(10,6) And the entity class is defined like this: public class Person { public int PersonId

NullReferenceException in EF 5-6.1.1 with two navigation properties to the same type

房东的猫 提交于 2019-12-05 17:50:48
I'd like to start with that I have a workaround for this issue - but I spent a few hours today figuring out the cause of the exception, so I'd thought I'd share Given two entities in the domain: public class User { public int Id { get; set; } public string Name { get; set; } } public class Ticket { public int Id { get; set; } public string Name { get; set; } public virtual User Owner { get; set; } public int? LockedByUserId { get; set; } public virtual User LockedByUser { get; set; } [Timestamp] public byte[] ETag { get; set; } } The following configuration: public class TicketConfiguration :

Self-referencing many-to-many relationship EF code first

限于喜欢 提交于 2019-12-05 17:39:02
I work with ASP.NET MVC With Durandal/Breeze templates. Let's say I have the following class: public class Person { public int Id { get; set; } public string Firstname { get; set; } public string Lastname { get; set; } public virtual List<Person> Friends { get; set; } } With the following EF Fluent API: modelBuilder.Entity<Person>() .HasMany(m => m.Friends) .WithMany() .Map(m => m.ToTable("Friends")); The database is generated successfully. The problem is when I perform a que ry with Breeze (client side) I have no data for the Friends property. var query = entityQuery.from('Person') .where('id

Second database created with MVC3, how to prevent?

╄→гoц情女王★ 提交于 2019-12-05 16:46:55
I'm very new to MVC 3 and I'm having problem with the Code First approach. I created 2 models, a context, view, etc and everything works perfect (able to add, delete, etc.) But when I check in Microsoft SQL Management Studio, I see 2 databases. One of them is the one I asked for in my web.config. This one contain only the membership table <connectionStrings> <add name="RecettesMaison" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=RecettesMaison;User Id=user;Password=password;" providerName="System.Data.SqlClient"/> The second one has a strange name: "RecettesMaison.DB

EF Linq to entities query generating UNION for TPC CTP5 code-first entity

依然范特西╮ 提交于 2019-12-05 14:56:45
I have the following entities: public class User { public int Id { get; set; } public string Name { get; set; } } public class HappyUser : User { public bool IsHappy { get; set; } } I am configuring the entities using Table Per Concrete Type (TPC) in order to generate a User table and a HappyUser table. I want the HappyUser table to include the properties of the User class, and I don't want any relationship between the two tables. I configure the entities as follows: public class UserTest : DbContext { public DbSet<User> Users { get; set; } public DbSet<HappyUser> HappyUsers { get; set; }