gpt4 book ai didi

c# - 如何将 Entity Framework (代码优先)与动态创建的连接字符串和单文件数据库一起使用?

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:00 24 4
gpt4 key购买 nike

我正在使用代码优先的 SqlServerCompact Entity Framework ,并且动态生成连接字符串,因为它是在用户请求时生成的数据库。

我遵循了这个问题的建议:How do I programmatically set the connection string for Entity-Framework Code-First?但是,我仍然有问题,事实上我得到一个异常,提示模型中的架构与代码中使用的架构不同。

然而,这很明显,我希望从代码生成模式,所以我真的不明白为什么会这样

有人可以帮我吗?这是使用的代码:

NerdDinners.cs

class NerdDinners : DbContext
{
public static string CreateConnectionString(string dbPath = @"|DataDirectory|\NerdDinners.sdf")
{
SqlCeConnectionStringBuilder sqlConnection = new SqlCeConnectionStringBuilder();
sqlConnection.Password = "9023fase93";
sqlConnection.DataSource = dbPath;

EntityConnectionStringBuilder connection = new EntityConnectionStringBuilder();
connection.Metadata = @"res://*/NerdDinnersModel.csdl|res://*/NerdDinnersModel.ssdl|res://*/NerdDinnersModel.msl";
connection.Provider = "System.Data.SqlServerCe.3.5";
connection.ProviderConnectionString = sqlConnection.ToString();

return connection.ToString();
}

public NerdDinners() : base() { }
public NerdDinners(string nameOrConnectionString) : base(nameOrConnectionString) { }

public DbSet<Dinner> Dinners { get; set; }
public DbSet<RSVP> RSVPs { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<NerdDinners>(new NerdDinnersInitializer());
Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe");

base.OnModelCreating(modelBuilder);
}
}

Program.cs

class Program
{
static void Main(string[] args)
{
NerdDinners nerdDinner = new NerdDinners(NerdDinners.CreateConnectionString());
Console.WriteLine("Inizio test");

var res = from d in nerdDinner.Dinners
select d;

Console.WriteLine("Dinners:");
foreach (var item in res)
Console.WriteLine(item.ToString());
Console.WriteLine("End");

// Usato per terminazione
Console.ReadKey();
}
}

这是我的解决方案(图片):

Solution rapresentation

编辑 1:

错误是一个 MetadataException,它提示无效的模式

编辑 2:

NerdDinnersInitializer.cs

class NerdDinnersInitializer : DropCreateDatabaseAlways<NerdDinners>
{
public NerdDinnersInitializer() : base() { }

protected override void Seed(NerdDinners context)
{
var toadd = new List<Dinner>
{
new Dinner
{
Address = "bla",
DinnerID = 0,
EventDate = DateTime.Parse("2010-12-03"),
HostedBy = "mclink",
Title = "Notitle",
},
new Dinner
{
Address = "Fiwe",
DinnerID = 0,
EventDate = DateTime.Parse("2011-12-03"),
HostedBy = "ngi",
Title = "Bah",
},
};

toadd.ForEach(item => context.Dinners.Add(item));

context.SaveChanges();

base.Seed(context);
}
}

最佳答案

我不确定这是否适用于您的情况,但如果模型与当前现有的数据库架构不匹配,您可能会收到错误消息。你需要做类似的事情-

Database.SetInitializer<NerdDinners>(
new DropCreateDatabaseIfModelChanges<NerdDinners>());

这将删除现有数据库并根据您的模型创建一个新数据库。

您的示例代码中确实有一些代码可以设置初始值设定项 -

Database.SetInitializer<NerdDinners>(new NerdDinnersInitializer());

但我不确定您的 NerdDinnersInitializer 类在做什么。

关于c# - 如何将 Entity Framework (代码优先)与动态创建的连接字符串和单文件数据库一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7457207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com