今朝私はEF4コードに手を入れ始めました。別のクラスライブラリにPOCO、データコンテキスト、Initializerクラスを作成しました。これは通常のボイラープレートタイプのコードだと思います。 MVC3アプリケーションでクラスを参照し、Global.asaxで初期化子を設定します。アプリケーションを実行すると、次のような問題が発生します。
1.データベースが作成されていません初期化された値にアクセスしようとすると、明らかにデータがないため、エラーが発生します。エンティティフレームワークコードクラスライブラリの最初
誰が おかげ
(私はこれを学ぶ私の全体のクリスマスの一日を過ごしたと私はまだそれが:(仕事を得ることができない場合は恥だろう)THIものが動作するように取得する方法についてのポインタで私を助けてくださいpsの私は、ブレークポイントを挿入しようと、私は、アプリの初期化方法を打つが、それは私もそこにブレークポイントを追加していても初期化子における種子方法を打つことはありません!!
感謝。
初期化子クラス
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using F2AController.Models;
namespace F2AController.DataObjects
{
public class F2AInitializer : DropCreateDatabaseAlways<F2AContext>
{
protected override void Seed(F2AContext context)
{
var countries = new List<Country>
{
new Country(){ CountryName="Germany", Active = true},
new Country(){ CountryName="Britain", Active = true}
};
countries.ForEach(s => context.Countries.Add(s));
context.SaveChanges();
var providers = new List<Providers>()
{
new Providers(){ ProviderName="InfoBip", ContactDetails="Rturo Manovic", Active=true, MessageRates= new List<ProviderRates>(){new ProviderRates(){ CountryId=1, DateCreated=DateTime.Now, DateModified=DateTime.Now, Rate=0.05M, Active=true}}}
};
providers.ForEach(p => context.Providers.Add(p));
context.SaveChanges();
var usermobiles = new List<MobileTerminal>()
{
new MobileTerminal(){ Active= true, Credits=200, DateCreated=DateTime.Now, MSISDN="4477565444865"}
};
usermobiles.ForEach(u => context.MobileTerminals.Add(u));
context.SaveChanges();
}
}
}
コンテキストクラス
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace F2AController.Models
{
public class F2AContext : DbContext
{
public DbSet<Country> Countries;
public DbSet<MobileTerminal> MobileTerminals;
public DbSet<Providers> Providers;
public DbSet<ProviderRates> ProviderRates;
public DbSet<Property> Properties;
public DbSet<ShortMessage> ShortMessages;
public DbSet<UserProperties> UserProperties;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
Global.asaxのアプリケーションの初期化方法
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
Database.DefaultConnectionFactory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["F2AContext"].ConnectionString);
Database.SetInitializer<F2AContext>(new F2AInitializer());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
イニシャライザクラスの投稿はどうですか?少なくともコンテクストクラスのいずれかである。 – LueTm