SQL Server Create database with Entity Framework

vddsk6oq  于 2023-03-07  发布在  其他
关注(0)|答案(1)|浏览(160)

I'm trying to create a database called Todo with Entity Framework. I want this database to appear in Sql Server Management after it has been created.

I have the context file:

namespace Domain
{
    //Associate the model with the database
    //This class then automatically defines a property for each table in the database that I want to work with.
    public class EFDbContext : DbContext
    {
        public EFDbContext() : base("EFDbContext")
        {
        }
        public DbSet<List> Lists { get; set; }
        public DbSet<Task> Tasks { get; set; }
    }
}

I have my Initializer:

namespace Domain
{
    public class ListRepository : System.Data.Entity.DropCreateDatabaseIfModelChanges<EFDbContext>
    {
        protected override void Seed(EFDbContext context)
        {
            var todo = new List<List>
            {
                new List { Day="Månadg" },
                new List { Day="Tisdag" },
                new List { Day="Onsdag" },
                new List { Day="Torsdag" },
                new List { Day="Fredag" }
            };

            todo.ForEach(t => context.Lists.Add(t));
            context.SaveChanges();
        }
    }
}

And here I have my Web.Config:

<connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Todo;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
  </connectionStrings>

    <contexts>
      <context type="todo.domain.EFDbContext, todo">
        <databaseInitializer type="todo.domain.ListRepository, todo"></databaseInitializer>
      </context>
    </contexts>

When I run my application, I Data Connections Is created In Server Explorer as In the picture below:

When I click on this Data Connection, I get the following error message:

When I check In Sql Server Management, no database is listed there. What am I doing wrong here? Why is the not database created?

As you can see, I have a solution called Todo, and In the Todo-solution, I have three projects. I have the DbContext In the Domain-project.

polhcujo

polhcujo1#

From your screenshot, it says login failed for user Bryan in your message. This would mean Bryan is unable to connect

Check the following 3 items

  1. From your Sql Management studio, open the Db, right icons and scroll down to security, (in windows authentication mode, usually easiest) and scroll down to security and then add Bryan as a user giving him DBO rights
  2. Authentication type Check is Mixed Mode authentication is enabled
  3. Switch the connection to Authentication and try with SA role

相关问题