gpt4 book ai didi

c# Azure Mobile Apps - 添加新模型不起作用

转载 作者:行者123 更新时间:2023-12-03 04:27:07 28 4
gpt4 key购买 nike

我创建了一个 Azure 移动应用程序,然后下载了生成的后端和移动应用程序。当我启动它时,它成功获取数据库中的 2 个待办事项。但是,一旦我添加如下用户模型 - 它就会停止在移动应用程序中获取待办事项。我不知道是什么导致了冲突。

  • 如果我删除用户模型和引用,我可以再次在移动应用上检索待办事项

当我从 DBcontext 中注释掉这一行时,它再次起作用:

//public DbSet<User> Users { get; set; }

启动:

public partial class Startup
{
public static void ConfigureMobileApp(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();

//For more information on Web API tracing, see http://go.microsoft.com/fwlink/?LinkId=620686
config.EnableSystemDiagnosticsTracing();

new MobileAppConfiguration()
.UseDefaultConfiguration()
.ApplyTo(config);

// Map routes by attribute
config.MapHttpAttributeRoutes();

// Use Entity Framework Code First to create database tables based on your DbContext
Database.SetInitializer(new AuthApp231Initializer());

// To prevent Entity Framework from modifying your database schema, use a null database initializer
// Database.SetInitializer<AuthApp231Context>(null);

MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();

if (string.IsNullOrEmpty(settings.HostName))
{
// This middleware is intended to be used locally for debugging. By default, HostName will
// only have a value when running in an App Service application.
app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
{
SigningKey = ConfigurationManager.AppSettings["SigningKey"],
ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] },
ValidIssuers = new[] { ConfigurationManager.AppSettings["ValidIssuer"] },
TokenHandler = config.GetAppServiceTokenHandler()
});
}
app.UseWebApi(config);
}
}

public class AuthApp231Initializer : CreateDatabaseIfNotExists<AuthApp231Context>
{
protected override void Seed(AuthApp231Context context)
{
List<TodoItem> todoItems = new List<TodoItem>
{
new TodoItem { Id = Guid.NewGuid().ToString(), Text = "First item", Complete = false },
new TodoItem { Id = Guid.NewGuid().ToString(), Text = "Second item", Complete = false },
};

foreach (TodoItem todoItem in todoItems)
{
context.Set<TodoItem>().Add(todoItem);
}

/*List<User> users = new List<User>
{
new User { Id = Guid.NewGuid().ToString(), Username = "adrian", Password = "supersecret" }
};

foreach (User user in users)
{
context.Set<User>().Add(user);
}*/

base.Seed(context);
}
}

数据对象:

public class User :EntityData
{

public string Username { get; set; }

public string Password { get; set; }
}

上下文:

public class AuthApp231Context : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to alter your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx

private const string connectionStringName = "Name=MS_TableConnectionString";

public AuthApp231Context() : base(connectionStringName)
{
}

public DbSet<TodoItem> TodoItems { get; set; }
public DbSet<User> Users { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(
new AttributeToColumnAnnotationConvention<TableColumnAttribute, string>(
"ServiceTableColumn", (property, attributes) => attributes.Single().ColumnType.ToString()));
}
}

最佳答案

据我所知,示例移动应用后端项目仅包含 TodoItem 模型,当您运行项目时,Database.SetInitializer(new AuthApp231Initializer()) 当数据库不存在时,将被触发并创建数据库和TodoItem表。如果在数据库初始化后添加其他数据模型,则需要迁移数据库。

注意:您可以利用 Add-Migration 添加自定义迁移,并使用 Update-Database 使数据库保持最新状态。此外,您还可以利用 MigrateDatabaseToLatestVersion 初始化程序自动升级数据库。

更多关于如何启用EF Code First迁移的详细信息,您可以引用这个官方tutorial .

关于c# Azure Mobile Apps - 添加新模型不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41923086/

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