简体   繁体   中英

How to use Entity Framework to just create a table in an existing SQL Server database?

I am currently trying to create a table with EF code-first in a SQL Server where I only have permissions to create tables in an existing database.

I cannot drop the entire database. I tried using migration update-table to let EF create the table for me, but because a __MigrationHistory table already exists (and is linked to different table), this didn't work.

Does anyone have a solution for my problem?

You need not to drop the whole database,

Add the class for the new Table, let's say your new Table is "person"

public class Person
{
   public int Id { get; set; }
   public string Name { get; set; }
}

and Add following line to DbContext

 public DbSet<Person> Persons { get; set; }

Now you need to Add this table to the existing Database. The easiest way is Go to Nuget Package Manager Console (Tools-> Nuget Package Manager -> Package Manager Console) inside visual studio

type Add-Migration AddedPersonTable

This will create a new entry in the Migration Table

and then type Update-Database

This will Add the new persons table to the existing database

Read here for More information about Migrations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM