「コードファースト」を使用する場合は、必要に応じてマッピングを作成できます。これは、作成したマッピングがデータベースと一致する場合、既存のデータベースでも機能します。
コンテキストを作成するたびに、マップする文字列(tablename)を作成できます。
いくつかの「最初のコード」のためのcodesamplesとどのように始めることができ:
DbContext:
public DbSet<YourEntity> YourEntities { get; set; }
...
// this is called when the db gets created and does the configuration for you => maybe not needed in your case
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
ConfigurationRegistrar configurationRegistrar = modelBuilder.Configurations;
new GeneralEntitiesConfiguration(configurationRegistrar);
}
GeneralEntitiesConfigurationがどのように見えるヘルパー以外の何物でも、コンフィギュレーションを処理するために使用してクラスイムではありません:
public class GeneralEntitiesConfiguration
{
public GeneralEntitiesConfiguration(ConfigurationRegistrar configurationRegistrar)
{
configurationRegistrar.Add(new YourEntityConfiguration());
//and additional configurations for each entity, just to splitt it a bit and have it more read and maintenance able
}
}
YourEntityConfigurationは、私はこのエンティティのすべての設定を持っているクラスです。
public class YourEntityConfiguration : EntityTypeConfiguration<YourEntity>
{
public YourEntityConfiguration()
{
ToTable("WhatEverYouLike"); // here you can do any magic to map this entity to a table, just make sure that your properties are mapped to the correct colums
Property(entity => entity.Id).HasColumnName("YouColumnName");
//and here you also have to do the other configurations
}
}
アプリケーションの起動時(または最初にコンテキストを初期化する前)に、データベースを初期化する必要があります。したがって、データベースをチェックして差異を処理するイニシャライザを使用することができます。 "DropCreateDatabaseAlways"や "DropCreateDatabaseIfModelChanges" =>のようなものがありますが、違いを無視する独自のものを作成する必要があります。私のサンプルでは、私は(私は最初の試行のためのsciptsとモデルの変更を処理したかった)モデルが異なるだけで例外をスローを作成しています
//before using the context the first time i'm calling, you can ignore the connection string
DbContextInitializer.Init(conString);
public static class DbContextInitializer
{
public static void Init (string connectionString)
{
Database.SetInitializer(new CreateDbThrowExceptionIfModelDiffersInitializer<SMDbContext>());
using(var dbContenxt = new MyDbContext(connectionString))
{
try
{
dbContenxt.Database.Initialize(true);
}
catch(DatabaseModelDiffersException diffException)
{
// some magic...
}
catch(Exception ex)
{
// TODO: log
throw;
}
}
}
public class CreateDbThrowExceptionIfModelDiffersInitializer<TContext> : IDatabaseInitializer<TContext> where TContext : DbContext
{
public void InitializeDatabase(TContext context)
{
using (new TransactionScope(TransactionScopeOption.Suppress))
{
if (!context.Database.Exists())
context.Database.Create();
}
if (!context.Database.CompatibleWithModel(true))
{
throw new DatabaseModelDiffersException("Database Model differs!");
}
}
protected virtual void Seed(TContext context)
{
// create data if you like
}
}
// just an exception i'm using for later useage
public class DatabaseModelDiffersException : Exception
{
public DatabaseModelDiffersException(string msg) : base(msg)
{}
}
}
はあなたのアイデアは、動的テーブルを扱うことができる持っている願っていますエンティティフレームワークの名前!
すべてのユーザーがすべての「エンティティ」(テーブルのセット)を作成しているわけではないので、マッピングはデータベースと一致する必要はありません。完全に一致することは必須ですか?とにかく、私はそれらのサンプルを感謝します。 – CarlosJ
週末に投稿します! –
ありがとう、ありがとうございます。 – CarlosJ