Answer the question
In order to leave comments, you need to log in
EF CodeFirst: What causes an error when creating a DB (doesn't want to create)?
Hello. I'm trying to create a database:
There are 2 models:
public class Player
{
public int PlayerId { get; set; }
public int TeamId { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public int Age { get; set; }
public virtual Team Team { get; set; }
}
public class Team
{
public int Id { get; set; }
public string Name { get; set; }
public string Coach { get; set; }
public List<Player> Player { get; set; }
}
public class DbEntity : DbContext
{
public DbSet<Player> Players { get; set; }
public DbSet<Team> Teams { get; set; }
}
Database.SetInitializer(new CreateDatabaseIfNotExists<DbEntity>());
<connectionStrings>
<add name="DbEntity"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename='|DataDirectory|\Footbal.mdf';Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Answer the question
In order to leave comments, you need to log in
Database.SetInitializer(new CreateDatabaseIfNotExists<DbEntity>());
using (DbEntitycontext = new DbEntity())
{
var query = context.Players.ToList();
}
using (DbEntitycontext = new DbEntity())
{
context.Database.Create();
}
Um... I'm not at all sure that the connection string should be written like this:
MyDatabase - context class name
InitalCatalog=MyDb - database name.
And as correctly written above, to create a database, you need to perform some action with it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question