ef-migrations

How to associate one table to many parents using EF Code First

时光毁灭记忆、已成空白 提交于 2019-12-05 20:37:34
I am building a domain model that requires several tables to be able to be references by more than one possible parent table. Something like you might have a table to store notes or files and those notes and/or files can be associated with different parent entities. Not that the same "file" or "note" can be associate with multiple owners, but that of 10 rows in a "files" table, 3 of them might be owned by rows from a "Customer" table, 3 of them might be owned by rows from an "Orders" table, and 4 of them might be owned by rows from a "Person" table. The owning tables all have virtual

MigrateDatabaseToLatestVersion no running Seed() method

一曲冷凌霜 提交于 2019-12-05 18:57:50
问题 I'm trying to automatically generate my database if it doesn't exists and run the Seed() method to populate the data. In my Database Context constructor I have this: Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, Configuration>()); This works great, my database is automatically created with all the tables as I want, but it seems like the Seed() method is not being called, my database is empty. This is my class: internal sealed class Configuration :

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 to specify a column Name for EF5 navigation property

时光毁灭记忆、已成空白 提交于 2019-12-05 17:42:58
I'm using EF5 code first to generate my database schema, but my new navigation property is being named in an undesirable way in the table. here is the model I'm working with. public class User { [Key] public long UserId { get; set; } ... **public virtual ICollection<ImagePermission> KeepThisNavigationName { get; set; }** } However, After I've updated my database and examine the table columns, the column is named: dbo.ImagePermission.User_UserId And I would like it to be named dbo.ImagePermission.KeepThisNavigationName_UserId I believe there is a way to do this using the Fluent API, but after

Could not load assembly Microsoft.EntityFrameworkCore.Design when running Add-Migration

旧巷老猫 提交于 2019-12-05 17:25:33
I have created a Class Library project with the following .csproj: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp1.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.sqlserver.Design" Version="1.1.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" /> </ItemGroup> </Project> When running the command: Add

How to create CreatedOn and UpdatedOn using EF Core 2.1 and Pomelo

我只是一个虾纸丫 提交于 2019-12-05 15:49:38
In Code First approach, how to define my entity so that: CreatedOn NOT NULL - value is generated on insert by the db with the current timestamp Updated NULL - value is generated on update by the db with the current timestamp Sample Entity: public class MyEntity { public int Id { get; set; } public string Name { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Column(TypeName = "TIMESTAMP")] public DateTime CreatedOn { get; set; } [Column(TypeName = "TIMESTAMP")] public DateTime UpdatedOn { get; set; } } DbContext: public class MyContext : DbContext { public MyContext

Code first migrations - what connection string will it use?

自闭症网瘾萝莉.ら 提交于 2019-12-05 11:51:55
问题 Code first migrations have been working very well for me. I have a services project and a wpf project. The model is in the services project which is referenced by the wpf project. Update-database is done on the services project, but uses connection string from the wpf project. I now add a web project which also references the service project. So now that there is a connection string in the app.config and there is one in the web.config, which one will it use? 回答1: In my scenario, the app

EF AddOrUpdate Seed does not Update Child Entities

▼魔方 西西 提交于 2019-12-05 11:51:13
问题 I'm having some issues Seeding data and I was able to reproduce the issue with a very small application. Given you have this Seed Method: protected override void Seed(JunkContext context) { context.Junks.AddOrUpdate(x => x.Name, new Junk() { Name = "BANANAS!!", Item = new JunkItem() { Name = "APPLES!!!" } } ); } when you run update-database in the PMC all of the entities get created successfully. Good. But when you wish to go and update the database, say your seed method is now this:

Is it acceptable to put seed data in the OnModelCreating method in EF code-first?

吃可爱长大的小学妹 提交于 2019-12-05 09:04:06
I know how to do seed data with the migrations API in EF 4.3. I've been playing with that all night. However, my ultimate goal is to get my project to the point where a user can pull it from source control and press F5 and they are good to go, database, seed data, and all. Currently code-first does an excellent job of building the DB on a fresh build but seed data isn't inserted until I do Update-Database in the package manager console. At that point it runs the seed method and inserts my seed data. Is it okay to just do that in the OnModelCreating method? Can I still take advantage of the

Code-first Entity Framework - Multiple Foreign Keys For the Same Table

拟墨画扇 提交于 2019-12-05 06:15:31
I have a Holiday table and a User table. The Holiday table has columns RequesterID and AuthorisedByID , which both link to the primary key of the User table. This is my Holiday model: public class Holiday { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid HolidayId { get; set; } [ForeignKey("UserId")] public virtual User User { get; set; } public Guid RequesterId { get; set; } public Guid? AuthorisedById { get; set; } } I am unable to declare the AuthorisedByID as a foreign key in the User table just like I think I did with RequesterId column. I wonder if you can give me