【发布时间】:2021-12-14 10:21:26
【问题描述】:
我遇到了实体框架的问题。我设法缩小了范围。
我有一个 Web 项目(带有 SQL 实体框架 6 的 Asp.net MVC 5)。我创建了一个简单的测试项目来执行此操作(它仅引用 Web 项目和所需的实体框架 nuget):
using (var context = new ApplicationDbContext())
{
context.Database.Delete();
context.Database.Create();
}
一切正常。现在,我有一个带有 SQLite 的 Xamarin 项目。我在这个项目中没有使用实体框架。我想在我的测试项目中引用它来测试移动代码和网站代码之间的交互。 一旦我引用 Xamarin 项目,测试就会失败
Test method Tests.UnitTest1.DbTest2 threw exception:
System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:
Website.Models.ApplicationDbContext.xxx: : EntityType 'Xx' has no key defined. Define the key for this EntityType.
xxxs: EntityType: EntitySet 'xxxs' is based on type 'Xx' that has no keys defined.
总结一下,我有 3 个项目:
- Web 项目(使用 Entity Framework 6 for SQL)
- 单元测试项目
- 使用 SQLite 的 Xamarin 项目
如果单元测试不引用 Xamarin 项目。测试没问题。
如果单元测试引用 Xamarin 项目。测试失败。
如果我不使用 Sqlite 引用 Xamarin 项目,该模型可以正常工作。然而,它就在这里。
public class Xx
{
public Xx()
{
}
[Key, ForeignKey("ApplicationUser")]
public string ApplicationUserId { get; set; }
[UIHint("UserProfile")]
[JsonIgnore]
public virtual ApplicationUser ApplicationUser { get; set; }
}
【问题讨论】:
-
“测试移动端代码和网站代码的交互”——这不是单元测试
-
@Jason 这更像是行为驱动测试。
标签: c# sql sqlite entity-framework xamarin