English 中文(简体)
EF4 法典 第一期数据库生成
原标题:EF4 Code First database generation

我完全是实体框架的开端人,我困惑不解,不可能使实体框架产生数据库。

我的背景是:

 public class ChatContext : DbContext
    {
        public ChatContext()
            : base("ChatContext")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ChatRoom>()
                         .Map(c => c.ToTable("chat_rooms"));

            modelBuilder.Entity<ChatUser>()
                        .HasMany(u => u.Rooms)
                        .WithMany(r => r.Users)
                        .Map(c => c.ToTable("chat_users"));

            modelBuilder.Entity<ChatUser>()
                       .HasMany(u => u.ConnectedUsers);

            base.OnModelCreating(modelBuilder);
        }

        public DbSet<ChatUser> Users { get; set; }
        public DbSet<ChatRoom> Rooms { get; set; }
        public DbSet<ChatMessage> Messages { get; set; }    
    }

我的全球协会:

public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

        protected void Application_Start()
        {
            Database.SetInitializer<ChatContext>(new DropCreateDatabaseIfModelChanges<ChatContext>());

            RegisterRoutes(RouteTable.Routes);
        }
    }

我在网上的联系:

<connectionStrings>
    <add name="ChatContext" connectionString="Data Source=.SQLEXPRESS;Initial Catalog=ChatContext;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

Can anyone spot where the problem lies? When I build the project, no errors are returned. A break point in Global.axax shows it hits the Application_Start().

最佳回答

你们需要回答问题。 如果数据库存在,则将进行核对,然后建立数据库。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签