Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code First Migrations With Existing Table

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 exists and it's done by another ORM, I need to say Migrations not to try to create Language table but update only Description table. How?

-"There is already an object named 'Languages' in the database."

like image 795
Evgeniy Avatar asked Aug 23 '12 09:08

Evgeniy


People also ask

How do I code my first migration to an existing database?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.

How do you use code first when an existing database schema?

To use code-first for an existing database, right click on your project in Visual Studio -> Add -> New Item.. Select ADO.NET Entity Data Model in the Add New Item dialog box and specify the model name (this will be a context class name) and click on Add.

How do I update my database in migration?

After creating a migration file using the add-migration command, you have to update the database. Execute the Update-Database command to create or modify a database schema. Use the –verbose option to view the SQL statements being applied to the target database.


1 Answers

-IGNORECHANGES

Scaffolds an empty migration ignoring any pending changes detected in the current model. This can be used to create an initial, empty migration to enable Migrations for an existing database. N.B. Doing this assumes that the target database schema is compatible with the current model.

http://coding.abel.nu/2012/03/ef-migrations-command-reference/

like image 52
Evgeniy Avatar answered Sep 28 '22 09:09

Evgeniy