我有一个Booking类,它有一个预订联系人(a Person)和一组导航属性(People),它们通过连接表链接到另一组导航属性(Bookings)Person.如何Booking为预订联系人关系启用级联删除生成表格?当我将它从流畅的API代码中删除时(启用了级联删除的默认设置),我从迁移中收到以下错误消息:
在'BookingPeople'表上引入FOREIGN KEY约束'FK_dbo.BookingPeople_dbo.People_PersonID'可能会导致循环或多个级联路径.指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束.
无法创建约束或索引.查看以前的错误.
modelBuilder.Entity<Person>()
.HasMany<Booking>(s => s.aBookings)
.WithRequired(s => s.Contact)
.HasForeignKey(s => s.ContactId);
modelBuilder.Entity<Booking>()
.HasMany(t => t.People)
.WithMany(t => t.Bookings)
.Map(m => {
m.ToTable("BookingPeople");
m.MapLeftKey("BookingID");
m.MapRightKey("PersonID");
});
Run Code Online (Sandbox Code Playgroud) 我有一个使用实体框架6-代码优先方法的MVC ASP.NET应用程序。
使用Fluent API,如何使用ASC / DESC排序在每列不同的多个列上添加索引?
我已经看到了许多使用多个列的示例,但是没有办法设置索引中列的排序顺序。
Table
-----
Id
Type
DateFor
DateCreated
Value
Run Code Online (Sandbox Code Playgroud)
我要在以下列上建立索引:Type(ASC),DateFor(Desc),DateCreated(Desc)。
indexing entity-framework ef-code-first entity-framework-6 ef-fluent-api
我Entity Framework 4.1 code first用来连接已经存在的数据库.我首先使用的表被称为Bank.我也有一个Bank class作为我的域模型.这是我映射我的类和表的方式:
public class HbfContext : DbContext
{
public DbSet<Bank> Banks { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Bank>().ToTable("Bank");
}
}
Run Code Online (Sandbox Code Playgroud)
我的银行表:
BankID INT
BankName VARCHAR(50)
Run Code Online (Sandbox Code Playgroud)
My Bank类看起来像这样:
public class Bank
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我想要归还所有银行时,我遇到了问题.从以下位置返回的SQL语句:
return db.Banks
.OrderBy(x => x.Name);
Run Code Online (Sandbox Code Playgroud)
是:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS …Run Code Online (Sandbox Code Playgroud) 我有一个ArticleComment实体,如下所示:
public class ArticleComment
{
public int ArticleCommentId { get; set; }
public int? ArticleCommentParentId { get; set; }
//[ForeignKey("ArticleCommentParentId")]
public virtual ArticleComment Comment { get; set; }
public DateTime ArticleDateCreated { get; set; }
public string ArticleCommentName { get; set; }
public string ArticleCommentEmail { get; set; }
public string ArticleCommentWebSite { get; set; }
public string AricleCommentBody { get; set; }
//[ForeignKey("UserIDfk")]
public virtual ApplicationUser ApplicationUser { get; set; }
public Guid? UserIDfk { get; set; …Run Code Online (Sandbox Code Playgroud) 整个概念如下:
表 1:国家:除了它自己的字段,它还包含两个 ICollection,其中一个指向地区集合,另一个指向城市。
表 2:区域 除了它自己的字段外,它还包含一个 CountryId 和一个 ICollection of Cities。
表 3:城市 除了它自己的字段外,它还包含一个 CountryId 和一个 RegionId(其中 regionId 可以为空)。
主要思想是一个国家可以有地区或/和城市。这意味着从地区/城市到 CountryId 的每个外键都不能为空。但是,允许从城市到地区的外键为空,因为“一些”国家,在这种情况下,地区是不必要的。
观察到所有三个表也使用 ICollection 引用一个中间表,该表依次存储它们之间的关系。
国家实体:
public class Country
{
public Guid Id { get; set; }
public int CountryId { get; set; }
public Guid ComponentId { get; set; }
public string NumCode { get; set; }
public string Code2 { get; set; }
public string Code3 { get; set; }
public virtual ICollection<Region> Regions { get; …Run Code Online (Sandbox Code Playgroud) 我在我的项目中的两个实体Student和Teacher它共享一个公共基类AccountModel。基类包含学生和教师都需要的属性(从语义上讲,学生和教师都是帐户持有者,为了良好的实践,这可以防止违反 DRY 原则)
在我的 Fluent API 配置中,我有:
builder
.Ignore<AccountModel>();
builder
.Entity<Student>()
.HasBaseType<AccountModel>()
.ToTable("Students");
builder
.Entity<Teacher>()
.HasBaseType<AccountModel>()
.ToTable("Teachers");
Run Code Online (Sandbox Code Playgroud)
但是,当 EF 构建迁移并生成新数据库时,我得到了一个AccountModel表,但没有一个StudentsorTeachers表。是什么赋予了?
c# entity-framework fluent entity-framework-core ef-fluent-api
我在使用Entity Framework 6.1从PayGroup对象获取对employee对象的引用时遇到问题.我在PayGroup.SupervisorId - > Employee.EmployeeId上的数据库中有一个外键.请注意,这是一个零或一对一的关系(薪酬组只能有一个主管,一个员工只能是一个薪酬组的主管).
根据GitHub上的这篇文章,在具有不同主键的表上不可能有外键.我手动将外键添加到数据库但我无法弄清楚如何设置流畅的api映射以便能够从薪酬组获取员工对象.
薪酬组表
员工表
注意:PayGroup.SupervisorId中有一个外键 - 数据库中的Employee.EmployeeId.
下面是DTO(我目前在这些类之间没有任何工作关系映射):
public class PayGroup
{
public int Id { get; set; }
public string SupervisorId { get; set; }
public virtual Employee Supervisor { get; set; }
}
public class Employee
{
public string EmployeeId { get; set; }
public string FullName { get; set; }
}
Run Code Online (Sandbox Code Playgroud) c# sql-server entity-framework entity-framework-6 ef-fluent-api
我正在使用EF6代码优先的方法来创建数据库。当我添加迁移和更新数据库时,它总是Non-cluster Index默认为表中的每个外键创建。
我的问题: EF6 是否有任何全局设置不能Non-Cluster index在外键上创建?
我已经搜索并找到了以下解决方案
解决方案 1 不适合我,因为我有很多表,而且我的表db已经创建好了。手动删除索引创建行需要很多时间。
此外,我也在使用fluent api是否有与此问题相关的任何选项?
我一直在搜索 EF Core 文档,如果在实体映射上添加 .HasIndex() 会给 DbFirst 场景带来任何好处,我找不到任何东西。
我有这个 20yo 数据库,其中创建了所有必要的表和索引,我正在映射一些表以使用 EF Core 查询它们。我想知道,在永远不会通过代码更新表架构的 DbFirst 场景中映射索引有什么好处?它会影响 EF 生成 SQL 查询的方式吗?
我指定了我在 Startup.cs 文件的实体项目中创建的上下文类和我为 connectionString 创建的 connectionString 数据。但是为什么我会收到这个错误?
错误消息:严重性代码描述项目文件行抑制状态错误 CS0311 类型“Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext”不能用作泛型类型或方法“EntityFrameworkServiceCollectionExtensions.AddDbContext(IServiceCollection, Action, ServiceLifetime, ServiceLifetime)'。没有从“Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext”到“Microsoft.EntityFrameworkCore.DbContext”的隐式引用转换。EntityFramework2 C:\Users\xsamu\source\repos\EntityFramework2\EntityFramework2\Startup.cs 29 活动
启动类:
namespace EntityFramework2
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<UserContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));
}
// This method gets called by the runtime. Use this method to configure the HTTP …Run Code Online (Sandbox Code Playgroud)