NCommon 1.1和EF4

时间:2011-03-01 22:31:20

标签: entity-framework-4 ncommon

我是NCommon的新手,正在寻找一个开始的示例项目。

我正在使用EF4。

我假设需要将EF4与POCO一起使用?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

     NCommon.Configure.Using(new StructureMapContainerAdapter(ObjectFactory.Container))
            .ConfigureState<DefaultStateConfiguration>()
            .ConfigureData<EFConfiguration>(config => config.WithObjectContext(
                 () =>
                 {
                     SiteContext db = new SiteContext(ConfigurationManager.ConnectionStrings["TestTheBest"].ConnectionString);
                     if (Transaction.Current != null )
                     {
                         db.Context.Connection.Open();
                     }
                     return db.Context;
                 }))
            .ConfigureUnitOfWork<DefaultUnitOfWorkConfiguration>(config => config.AutoCompleteScope());


public class SiteContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<Company> Companies { get; set; }
    public DbSet<Comment> Comments { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Test> Tests { get; set; }
    public DbSet<Client> Clients { get; set; }
    public DbSet<Question> Questions { get; set; }
    public DbSet<Section> Sections { get; set; }
    public DbSet<Answer> Answers { get; set; }
    public DbSet<GlobalSettings> GlobalSettings { get; set; }
    public DbSet<PassageTest> PassageTest { get; set; }
    public DbSet<PassageTestAnswer> PassageTestAnswer { get; set; }
    public DbSet<SaaSUser> SaaSUser { get; set; }

    public SiteContext(string nameOrConnectionString)
        : base(nameOrConnectionString)
    {
        Context.SavingChanges += new EventHandler(Context_SavingChanges);
    }

    void Context_SavingChanges(object sender, EventArgs e)
    {
        if(Context.Connection.State==ConnectionState.Open)
            Context.Connection.Close();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().HasMany(x => x.Invitations).WithRequired(x=>x.user).WillCascadeOnDelete(true);
        modelBuilder.Entity<User>().HasMany(x => x.PassageTests).WithRequired(x => x.user).WillCascadeOnDelete(true);

        modelBuilder.Entity<PassageTest>().HasMany(x => x.PassageTestAnswers).WithRequired(x => x.passageTest).WillCascadeOnDelete(true);

      //  modelBuilder.Entity<Company>().HasMany(x => x.Users).WithRequired(x => x.company).WillCascadeOnDelete(true);

        modelBuilder.Entity<Question>().HasMany(x => x.Answers).WithRequired(x => x.question).WillCascadeOnDelete(true);
        modelBuilder.Entity<Question>().HasMany(x => x.PassageTestAnswers).WithRequired(x => x.question).WillCascadeOnDelete(true);

        modelBuilder.Entity<Test>().HasMany(x => x.Invitations).WithRequired(x => x.test).WillCascadeOnDelete(true);
        modelBuilder.Entity<Test>().HasMany(x => x.PassageTests).WithRequired(x => x.test).WillCascadeOnDelete(true);
        modelBuilder.Entity<Test>().HasMany(x => x.Sections).WithRequired(x => x.test).WillCascadeOnDelete(true);

        modelBuilder.Entity<Client>().HasMany(x => x.Tests).WithRequired(x => x.client).WillCascadeOnDelete(true);
    }

    public ObjectContext Context
    {
        get { return ((IObjectContextAdapter)this).ObjectContext; }
    }
}