Я новичок в Entity Framework с кодом.
Сейчас я попробовал несколько вещей, но не могу заставить EF создать какие-либо таблицы в моей базе данных SQL Azure.Может кто-нибудь посоветовать некоторые шаги и настройки, которые я должен проверить.
У поставщика членства нет проблем при создании его таблиц.
Я добавил PersistSecurityInfo = True в строку подключения.Строка подключения использует основную учетную запись пользователя для сервера.
Когда я реализую таблицы в базе данных с помощью sql, все работает нормально.
У меня есть следующее в WebRole.cs
//Initialize the database
Database.SetInitializer<ReykerSCPContext>(new DbInitializer());
Мой DbInitializer (который не запускается до того, как я получаю «Недопустимое имя объекта 'dbo.ClientAccountIFAs'.", Когда я пытаюсь получить доступ к таблице в первый раз. Через некоторое время после запуска.
public class DbInitializer:DropCreateDatabaseIfModelChanges<ReykerSCPContext>
{
protected override void Seed(ReykerSCPContext context)
{
using (context)
{
//Add Doc Types
context.DocTypes.Add(new DocType() { DocTypeId = 1, Description = "Statement" });
context.DocTypes.Add(new DocType() { DocTypeId = 2, Description = "Contract note" });
context.DocTypes.Add(new DocType() { DocTypeId = 3, Description = "Notification" });
context.DocTypes.Add(new DocType() { DocTypeId = 4, Description = "Invoice" });
context.DocTypes.Add(new DocType() { DocTypeId = 5, Description = "Document" });
context.DocTypes.Add(new DocType() { DocTypeId = 6, Description = "Newsletter" });
context.DocTypes.Add(new DocType() { DocTypeId = 7, Description = "Terms and Conditions" });
//Add ReykerAccounttypes
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 1, Description = "ISA" });
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 2, Description = "Trading" });
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 3, Description = "SIPP" });
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 4, Description = "CTF" });
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 5, Description = "JISA" });
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 6, Description = "Direct" });
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 7, Description = "ISA & Direct" });
//Save the changes
base.Seed();
}
, и мой класс DBContext выглядит как
public class ReykerSCPContext : DbContext
{
//set the connection explicitly
public ReykerSCPContext():base("ReykerSCPContext"){}
//define tables
public DbSet<ClientAccountIFA> ClientAccountIFAs { get; set; }
public DbSet<Document> Documents { get; set; }
public DbSet<DocType> DocTypes { get; set; }
public DbSet<ReykerAccountType> ReykerAccountTypes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Runs when creating the model. Can use to define special relationships, such as many-to-many.
}
Код, используемый для доступа к:
public List<ClientAccountIFA> GetAllClientAccountIFAs()
{
using (DataContext)
{
var caiCollection = from c in DataContext.ClientAccountIFAs
select c;
return caiCollection.ToList();
}
}
, и в последней строке отображаются ошибки. Справка!