ef-migrations

How does Database.SetInitializer actually work? (EF code-first create database and apply migrations using several connection strings)

隐身守侯 提交于 2019-11-29 22:55:34
问题 I am trying to write a method to create a database and run migrations on it, given the connection string. I need the multiple connections because I record an audit log in a separate database. I get the connection strings out of app.config using code like ConfigurationManager.ConnectionStrings["Master"].ConnectionString; The code works with the first connection string defined in my app.config but not others, which leads me to think that somehow it is getting the connection string from app

Entity Framework code-first: migration fails with update-database, forces unneccessary(?) add-migration

北城余情 提交于 2019-11-29 22:00:47
I have a funny effect using migration (EF 5.0) and code-first: I created some models with GUID primary keys. (BTW: It is important for me, that SQL Server uses NEWSEQUENTIALID() , which seems to be the default value in the current version) At some point I activated migrations. I added some code to the initial migration, this is mostly .Index() as needed. When I delete the database and call update-database, I get the following error: Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes

How to migrate database schema from Identity 2.2.0 to 3.0.0-rc1-final

可紊 提交于 2019-11-29 21:20:30
问题 I've been playing around migrating an existing application running on MVC5 with ASP.NET Identity 2.2.0 having trouble finding proper information, i'd like to ask how you are supposed to do this migration? I've used a couple of hours on this problem and have found a solution that allows me to log in with Identity 3.0.0-rc1-final with ef7 on my old but migrated database. I will post what I did as an answer, but i'd very much like inspiration or other ways to have done it. 回答1: First I generated

Lose EF Code First Migration when working on different TFS branches?

风流意气都作罢 提交于 2019-11-29 20:18:05
We are using TFS and have different branches for our Dev. in the branch A we made a migration to change a column size in the branch B we made a migration to add a new table. This branch doesn not know about the branch A modification !! both modification are merged to the main branch. When I do an update database, it does the 2 migration but at the end tells me there is pending changes. If I do an Add-Migration, it creates the same as the 1st migration (in branch A). Unable to update database to match the current model because there are pending changes and automatic migration is disabled.

EF6 Code First - may cause cycles or multiple cascade paths

别来无恙 提交于 2019-11-29 19:42:06
问题 I'm using EF6 Code First. I have two classes: public class Player { [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] [Key] public int Id { get; set; } [Required, MinLength(2, ErrorMessage = "Player name must be at least 2 characters length")] public string Name { get; set; } [Required] public int TeamClubId { get; set; } [Required] public int TeamNationalId { get; set; } [Required, ForeignKey("TeamClubId")] public virtual Team Club { get; set; } [Required, ForeignKey(

How to correctly set foreign key constraint in one to many relationship in code first ASP.NET MVC 5 application

蓝咒 提交于 2019-11-29 18:01:20
So I have one document table and a ApplicationUser table. One user can upload multiple documents and a foreign key constraint has been applied accordingly using the code first approach. But the problem is, I am able to save the document without assigning the UserID foreign key id in the Documents table . What am I missing here? Below is my novice attempt. ApplicationUser Model: it's the standard model provided by the framework. Document Model: public class Document { public int Id { get; set; } [StringLength(255)] public string FileName { get; set; } [StringLength(100)] public string

Script EF migration seed from Configuration class

☆樱花仙子☆ 提交于 2019-11-29 16:08:07
问题 I have EF migrations working nicely, but I also want to generate the sql script for the seed data from my DbMigrationsConfiguration class. The seed data runs ok when I do Update-Database, but when I do UpdateDatabase -Script I do not get the sql for the seed inserts. I tried -Verbose on a normal Update-Database but I do not see the seed statements output there either. Is this possible? 回答1: No it is not possible. Configuration class is not part of migration itself - it is infrastructure

Getting 'Context is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.\"

旧城冷巷雨未停 提交于 2019-11-29 13:30:19
I am getting this error when I try to use code first migrations. My context has a constructor with the connection name. public class VeraContext : DbContext, IDbContext { public VeraContext(string NameOrConnectionStringName = "VeraDB") : base(NameOrConnectionStringName) { } public IDbSet<User> Users { get; set; } public IDbSet<Product> Products { get; set; } public IDbSet<IntCat> IntCats { get; set; } } This connection name is injected with ninject when the project runs, I have also specified it as a default as in the above code but this did not help. kernel.Bind<IDbContext>() .To<VeraContext>

stuck in EF migration limbo

让人想犯罪 __ 提交于 2019-11-29 13:07:42
i have somehow gotten my EF5 project into a state where I can't proceed. When I do an 'update-database' i get: Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model changes to a code-based migration. ok, fine, so i try to 'add-migration', and i get: Unable to generate an

Code First Migrations With Existing Table

跟風遠走 提交于 2019-11-29 12:56:35
问题 In my model I have navigation property Language: public class IntegratorDescription : BaseContract { [Key, Column(TypeName = "bigint"), DataMember] public long Id { get; set; } [DataMember, Column(TypeName = "bigint"), ForeignKey("Language")] public long LangId { get; set; } [DataMember] public string CompanyShortInfo { get; set; } [DataMember, Column(TypeName = "ntext")] public string CompanyInfo { get; set; } public virtual Models.Language Language { get; set; } } Language table already