开发者

Entity framework does not create table in DB

So first of all I dont know what Entity Framework version I am using. I am assuming its 4 but how do I check?

Second i have the following connection string in web.config:

  <connectionStrings>

<add name="DBEntites"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
     providerName="System.Data.SqlClient" />

<add name="ApplicationServices"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
     providerName="System.Data.SqlClient" />

As you can see they are the same DB. In fact they are the DB created by asp.net Membership Provider.

Now I have the following Model:

public class Profile {
    [Key]
    public string username { get; set; }

    [Display(Name = "Full Name")]
    public string FullName { get; set; }

}

Simple enough?

Then I have the following class that connects to the DB:

   public class DBEntities : DbContext {
        public DbSet<Profile> Profiles { get; set; }
    }

Then in my Account Controller, infact this is the same controller created by the VS2010 when you choose a new MVC3 project.

 [HttpPost]
 public ActionResult 开发者_StackOverflow Register(RegisterModel model) {
            if (ModelState.IsValid) {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
                Profile pm = new Profile();
                pm.username=model.UserName;
                pm.FullName="unknown";

                db.Profiles.Add(pm);
                db.SaveChanges();



                if (createStatus == MembershipCreateStatus.Success) {
                    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                } else {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

SO when i run this application, it runs through without exceptions. If I add a new user, the new user is added to the asp.net membership tables. So from my understanding the Entity framework should create a table called "Profile" with the two columns "username" and "fullname".

However, it dosn't. Why is it the code can step through without exceptions, the db.SaveChanges() can go through, yet there is not table created?

What am I doing wrong?

edit: I think the table is there beause if I try to create an entry with the same username (obviously can't since its a Primary Key), it throws an exception saying the entry already exists.

BUT I cant seem to find the table!


You are using EF 4.1 and the behavior is expected. You are using existing database and in such case EF will not create tables for you. EF with default features can only create database and all tables together. Membership API doesn't use EF so your second test fails on Membership (username must be unique) not on EF.

To solve your problem you must either create tables in the database manually or you must let EF to create the database which will be little bit harder because it will have to create all related ASP.NET tables for you. One way to do that is script those tables to external SQL file and execute that file in custom initializer. For custom intializer check this answer. This way doesn't like scripts containing GO command. If you want to use script with GO command you have to use SQL Server Management Objects to execute the script (SMO).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜