code-first

Entity Framework Code first mapping without foreign key

隐身守侯 提交于 2019-12-18 04:16:55
问题 I have two tables: Requirement ID (int) PK ClientID (int) JobNumber (int) Comment ID (int) PK Job_ID (int) Comment (varchar) The tables don't have foreign keys and there's no possibility of adding any. I'm trying to map them in EF. I have classes for each and I'm trying to define the relationship in fluent code to map the Comment.Job_ID to the Requirement.JobNumber. A requirement can have many comments. Requirement has a list of Comments and Comment has a Requirement property. I have this

LINQ to Entities does not recognize the method

纵然是瞬间 提交于 2019-12-18 04:16:08
问题 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); } }

Updating a reference to a child object in Entity framework 4.1 (CodeFirst)

夙愿已清 提交于 2019-12-18 03:34:20
问题 I'm trying to update an object that I have previously saved with EntityFramework 4.1 (CodeFirst) The class Job has the following properties ... public class Job { [key] public int Id { get; set; } public string Title { get; set; } public Project Project { get; set; } public JobType JobType { get; set; } public string Description { get; set; } } The initial create works fine, but the update only commits changes to the strings.. If I change the child objects eg the JobType Property from

Entity Framework: Tracking changes to FK associations

送分小仙女□ 提交于 2019-12-18 01:21:36
问题 I'm overriding SaveChanges on my DbContext in order to implement an audit log. Working with many-to-many relationships or independent associations is relatively easy as EF creates ObjectStateEntries for any changes to those kinds of relationships. I am using foreign key associations, and when a relationship between entities changes all you get is an ObjectStateEnty that says for example entity "Title" has "PublisherID" property changed. To a human this is obviously a foreign key in Title

Entity Framework: Tracking changes to FK associations

Deadly 提交于 2019-12-18 01:21:32
问题 I'm overriding SaveChanges on my DbContext in order to implement an audit log. Working with many-to-many relationships or independent associations is relatively easy as EF creates ObjectStateEntries for any changes to those kinds of relationships. I am using foreign key associations, and when a relationship between entities changes all you get is an ObjectStateEnty that says for example entity "Title" has "PublisherID" property changed. To a human this is obviously a foreign key in Title

How to prevent DbContext from altering the database?

时间秒杀一切 提交于 2019-12-17 23:43:02
问题 I'm learning Entity Framework (currently using EF6 beta) and I'm using the code first pattern on an existing database. The entities and DbContext class are created automatically using a T4 template. I would like to prevent the DbContext from creating / altering anything into the database at runtime. How can I do that? 回答1: When you do enable-migrations command, you will be presented with folder /Migrations under which you can find file named Configuration.cs . Within constructor of that file,

Change foreign key constraint naming convention

半世苍凉 提交于 2019-12-17 19:34:26
问题 We have our own external convention of naming objects and I need to change the naming convention for the auto generated foreign key constraints. Now it looks like: FK_dbo.City_dbo.CityType_City_CityTypeId but I would like it to be called City_FKC_CityType . I found a similar question which says that you can change the name of constraints manually. However, this does not suit me, since I have a lot of tables and foreign key constraints. I found some information about "Custom Code First

Returning a DataTable using Entity Framework ExecuteStoreQuery

前提是你 提交于 2019-12-17 18:54:44
问题 I am working with a system that has many stored procedures that need to be displayed. Creating entities for each of my objects is not practical. Is it possible and how would I return a DataTable using ExecuteStoreQuery ? public ObjectResult<DataTable> MethodName(string fileSetName) { using (var dataContext = new DataContext(_connectionString)) { var returnDataTable = ((IObjectContextAdapter)dataContext).ObjectContext.ExecuteStoreQuery<DataTable>("SP_NAME","SP_PARAM"); return returnDataTable;

in entity framework code first, how to use KeyAttribute on multiple columns

不羁岁月 提交于 2019-12-17 17:36:29
问题 I'm creating a POCO model to use with entity framework code first CTP5. I'm using the decoration to make a property map to a PK column. But how can I define a PK on more then one column, and specifically, how can I control order of the columns in the index? Is it a result of the order of properties in the class? Thanks! 回答1: You can specify the column order in the attributes, for instance: public class MyEntity { [Key, Column(Order=0)] public int MyFirstKeyProperty { get; set; } [Key, Column

Code First Migrations and Stored Procedures

≯℡__Kan透↙ 提交于 2019-12-17 15:51:07
问题 I have just created a database and done my first migration (just a simple table add). Now I want to add some stored procedures which I have just added by writing the sql and executing it in Management Studio. But I would like to include these stored procedures if possible in a migration so that they are saved and I can run an Up or Down method against them. Is this possible and if so what syntax needs to be used? Or will I just have to add/edit/remove them using Management Studio? 回答1: I've