code-first

Entity Framework CTP5 Code-First Mapping - Foreign Key in same table

て烟熏妆下的殇ゞ 提交于 2019-11-29 11:16:35
How would I map something like this using the modelBuilder? Where theres a nullable foreign key referencing the same tables primary key Table: Task taskID int pk taskName varchar parentTaskID int (nullable) FK Task class: public class Task { public int taskID {get;set;} public string taskName {get;set;} public int parentTaskID {get;set;} public Task parentTask {get;set;} } ... modelBuilder.Entity<Task>() .HasOptional(o => o.ParentTask).... The following code gives you the desired schema. Note that you also need to define ParentTaskID foreign key as a nullable integer, like I did below. public

Lazy vs eager loading performance on Entity Framework

我的梦境 提交于 2019-11-29 09:57:53
So I have the following model classes on my DbContext: Everytime I render a list of LoanApplication objects I do something like this: var context = new MyContext(); var applications = context.LoanApplications.Where(d => d.PropertyThatIWantToFilter = localVariable); This returns an IQueryable that then I convert to a ViewModel like this on my controller method call: var vm = applications.Select(d => new LoanApplicationViewModel(d)); Inside the LoanApplicationViewModel constructor I accept the entity object and do the corresponding mapping. The thing is that, since the Solicitors collection is a

Code First Fluent API and Navigation Properties in a Join Table

瘦欲@ 提交于 2019-11-29 07:12:31
I have four entities that I would like to translate into database tables via code first fluent api (I'm using a model found at databaseanswers.org), but I'm not certain as to how. The problem I'm having is that SuggestedMenuId is being migrated across two different tables in a Composite key (MenuCourse and CourseRecipeChoice). Here's the message I'm getting: "One or more validation errors were detected during model generation: \tSystem.Data.Entity.Edm.EdmAssociationConstraint: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical." Here

How to make persisted computed column in EF code first?

回眸只為那壹抹淺笑 提交于 2019-11-29 06:53:03
How do I get this column as similar to a PERSISTED COMPUTED column in the database? My current attempt (it loads all CompCol rows with null in seed) : public class Call { public Call() { } [Key] public int Id { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public string CompCol { get { return "ABC-" + Convert.ToString(Id).PadLeft(5, '0'); } protected set {} } } The solution I found was to : Make sure auto migrations are turned off. This is so that VS will generate a script (fluent api code) for us to further customise instead of just running it. So in the configuration

Code First adding to collections? How to use Code First with repositories?

时间秒杀一切 提交于 2019-11-29 05:52:10
EDIT: This happen only on larger scale projects with repositories. Is there anybody using EF4 with CodeFirst approach and using repositories? Please advise me. Hi. Im currently working with EF4 CodeFirst Classes. In my test project I got two classes, Author and Book (author got books). What I'm trying to do is that I have a AddBook in my Author class, but that wont seem to work like I can't Add it to the collection.. here are my classes and two different exceptions. public class Book { public virtual int BookId { get; set; } public virtual string Title { get; set; } public virtual Author

Error storing Image in SQL CE 4.0 with ASP.NET MVC 3 and Entity Framework 4.1 Code First

安稳与你 提交于 2019-11-29 05:37:21
I'm trying to store/save an image in an SQL Compact Edition (CE) database. I declare the field in my Student model as: [Column(TypeName = "image")] public byte[] Photo { get; set; } The database is created with the image data type for the Photo column as can be seen here: The problem is: When I run the app and try to save a Student with a Photo of 3 MB (for example), I get an exception: validationError.ErrorMessage = "The field Photo must be a string or array type with a maximum length of '4000'." SQL Server CE supports these Data Types . In this comparison between SQL Express and SQL Compact

Using Entity Framework Code First CTP5, how do I create a primary key column that are INTs and are not identity columns

你说的曾经没有我的故事 提交于 2019-11-29 05:25:36
Using Entity Framework Code First CTP5, how do I create a primary key column that are INTs and are not identity columns Preferably not using attributes. Matt Hamilton If you're building your entities using the fluent interface rather than using attributes over the properties, you may be able to use the DatabaseGenerationOption class (from EntityFramework.dll, in the System.ComponentModel.DataAnnotations namespace). I've not tested it, but this is what it would look like: modelBuilder .Entity<Foo>() .Property(f => f.Id) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); That answer didn

Entity Framework | Code First | Mapping of sub property from CultureInfo.Name

我只是一个虾纸丫 提交于 2019-11-29 05:17:25
I've got an entity like this : public class Course { public CultureInfo Culture {get; set;} : } And I want to map just the Name property of CultureInfo to a single column in the table that Entity Framework generates for me. How would I go about this? Currently I've tried looking at the OnModelCreating() method of the DbContext, but I'm not finding anything that is standing out. I'm using Entity Framework 4.1 in and MVC 3 application with a Code First approach. Many thanks. Unfrotunatelly EF can't do it this way. The workaround is defining separate property with the Name which will be mapped to

LINQ to Entities does not recognize the method

让人想犯罪 __ 提交于 2019-11-29 04:33:50
I am following this article on MSDN. I ported it to EF Code First. public interface IUnitOfWork { IRepository<Employee> Employees { get; } IRepository<TimeCard> TimeCards { get; } void Commit(); } public class HrContext : DbContext { public DbSet<Employee> Employees { get; set; } public DbSet<TimeCard> TimeCards { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Employee>() .HasMany(e => e.TimeCards) .WithOptional(tc => tc.Employee); } } public class SqlRepository<T> : IRepository<T> where T : class { private readonly DbSet<T> entitySet;

Force EF 4.1 Code First to See an Attached entity as Modified

 ̄綄美尐妖づ 提交于 2019-11-29 02:38:52
问题 All the examples I've found refer to a class called ObjectContext, which doesn't appear to exist in CTP5. I must stress at this point, CTP5 is my first exposure to the Entity Framework. I have a disconnected POCO that I have attached to my DbContext. SaveChanges does not pick up the change though, how I tell my context to update that entity? _context.Users.Attach(user); // The user has been replaced. _context.SaveChanges(); // The change is not saved. What am I doing wrong? Update 12/01/2011