我的项目使用 EF Code First 运行得很好。但现在我需要在数据库中添加 1 个字段(例如 DateTime 类型的 CreateDate),该字段可以为 null。
因此,我编写了脚本以向 DB(CreateDate (datetime) null ) 添加 1 个字段,然后修改模型以包含新字段
public class Account()
{
...
public DateTime CreateDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
下面是使用 FluentApi 创建此字段选项的代码
Property(x => x.CreateDate).IsOptional();
Run Code Online (Sandbox Code Playgroud)
问题是当我尝试获取帐户的任何实例时,当我修改字段时出现错误,如下所示:
public DateTime? CreateDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
然后就可以了。
你们能给我解释一下吗?为什么我的 Fluent api 不起作用或者我做错了什么?
谢谢。
我知道已经有几个关于此的问题,但没有一个真正解决我的问题:
应用背景
我有一个首先使用 Entity Framework 6.1 Code 的 Web 应用程序。我在 DAL 中使用存储库模式,这意味着我有存储库来查询我的上下文并将结果返回到服务,然后服务使用自动映射器将我的实体映射到视图模型,然后返回虚拟机。服务通过 Web API 方法调用。
基于上述架构,很明显我正在使用独立的实体。
我遇到的问题是更新(PUT)现有实体。流程如下所示:
Angular 向 WebAPI 方法发出 HTTP PUT,将 VM 作为参数传递。然后,WebAPI 调用 Service 方法,并将 VM 作为参数传递。服务方法使用自动映射器将 VM 转换为 EF 实体 服务方法然后调用相应的存储库,将分离的 EF 实体的新实例作为参数传递。
服务方法示例:
public async Task<List<DependantViewModel>> SaveDependants(int customerId, List<DependantViewModel> dependantViewModels)
{
var dependantEntities = Mapper.Map<List<DependantViewModel>, List<Dependant>>(dependantViewModels);
bool result = false;
foreach (var dependantEntity in dependantEntities)
{
result = await _dependantRepository.InsertOrUpdate(dependantEntity);
if (result != true)
{
// log errror
}
}
return Mapper.Map<List<Dependant>, List<DependantViewModel>>(dependantEntities);
} …Run Code Online (Sandbox Code Playgroud) 我使用实体框架 6 在 MVC 5 中创建一个项目。我使用的是代码优先方法。我想在其中一个模型中为表定义一个与默认名称不同的名称。为此,我使用 System.ComponentModel.DataAnnotations 名称空间并定义该类,如下所示:
[Table(Name="Auditoria")]
public class AuditoriaDAL
{
[Key]
public int AuditoriaId { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
运行该项目,我得到一个数据库,其中有一个名为 AuditoriaDALs 的表。为什么表的名称不是我定义的名称?
asp.net-mvc data-annotations ef-code-first entity-framework-6
我收到的错误消息如下 ----- MVCRegistration.Models.Post_UserProfile: : 多重性与关系“Post_UserProfile”中角色“Post_UserProfile_Target”中的引用约束冲突。由于从属角色中的所有属性均不可为空,因此主体角色的重数必须为“1”。MVCRegistration.Models.PostComment_UserProfile::多重性与关系“PostComment_UserProfile”中角色“PostComment_UserProfile_Target”中的引用约束冲突。由于从属角色中的所有属性均不可为空,因此主体角色的重数必须为“1”。 这里,MVCRegistraion 是解决方案文件名。
现在,我有这样的三堂课——
public class UserProfile
{
public UserProfile()
{
this.PostComments = new HashSet<PostComment>();
this.Posts = new HashSet<Post>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string AvatarExt { get; set; }
public virtual ICollection<PostComment> PostComments { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
后期课程是这样的----
public class Post
{
public Post()
{
this.PostComments = new HashSet<PostComment>();
}
[Key]
public int …Run Code Online (Sandbox Code Playgroud) 我使用 EF 开发框架。我想获取实体的所有被忽略的属性来构建一些特殊查询。我该怎么做?
public class Customer
{
public int Id { get; set; }
public DateTime BirthDate { get; set; }
public int Age { get; set; }
}
public class CustomerContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().Ignore(customer => customer.Age);
base.OnModelCreating(modelBuilder);
}
public DbSet<Customer> Customers { get; set; }
}
public static class DbContextExtensions
{
public static List<string> GetIgnoredProperties(this DbContext context, string entityTypeName)
{
// ???
}
}
Run Code Online (Sandbox Code Playgroud) c# entity-framework ef-code-first entity-framework-5 entity-framework-6
我在我的数据模型中添加了这个计算列
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string FullName { get; private set; }
Run Code Online (Sandbox Code Playgroud)
之后,我使用此查询在我的数据库中创建了它
ALTER TABLE [MyDataBase].[dbo].[User] ADD FullName as ([FirstName] + ' ' + [LastName])
Run Code Online (Sandbox Code Playgroud)
当我运行我的代码时,我收到一个错误,指出我的数据库已更改。我的问题如何为这个计算列创建迁移(因为它已经使用 sql 查询创建了)
当我生成迁移时,我有以下实体,它会创建两个名为 RestrictedCategoryId 和 RestrictedCategoryId1(FK) 的列。如何解决这个问题,只用 FK 生成一列?
注意:我需要每个实体中的 OrderId。
`C#
public class Order
{
public Guid Id { get; set; }
public DateTime OrderDate { get; set; }
private List<Category> _categories;
public List<Category> Categories => _categories;
}
public class Category
{
public Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public Guid OrderId { get; set; }
public Order Order { get; set; }
private List<RestrictionCategory> _restrictedCategories;
public …Run Code Online (Sandbox Code Playgroud) sql-server ef-code-first entity-framework-core ef-fluent-api .net-core
文档说:该上下文的模型被缓存,并且适用于应用程序域中上下文的所有其他实例。可以通过在给定的 ModelBuidler 上设置 ModelCaching 属性来禁用此缓存
但我找不到办法做到这一点。我必须禁用缓存,因为我在运行时添加模型并从程序集中加载所有模型并创建数据库。
我发现此链接表示实现此目的的一种方法是使用 DBModelBuilding - 手动将模型添加到上下文中,但它适用于实体框架,对 EF Core 没有帮助。
我希望有人能解决这个问题。
谢谢
我首先使用EF4代码,并希望生成一个由类属性和外键组成的复合键.我有两个班:订单和公司.Order类包含引用,但这在公司之间不一定是唯一的.所以我打算使用由Reference和Company.CompanyId组成的复合键.
我已尝试使用以下设置它但我收到错误消息"键表达式无效".
modelBuilder.Entity<Order>().HasKey(o => new { o.Reference, o.Company.CompanyId });
Run Code Online (Sandbox Code Playgroud)
我也试过了
modelBuilder.Entity<Order>().HasKey(o => new { o.Reference, o.Company });
Run Code Online (Sandbox Code Playgroud)
这失败了.
这些是我的课程:
public class Order
{
public string Reference { get; set; }
public Company Company { get; set; }
}
public class Company
{
public int CompanyId { get; set; }
public virtual ICollection Orders { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
我真的想尝试使用绿地应用程序尝试实体框架4 Code First CTP,但我想确保地理空间搜索在我做之前不会成为问题,或者我会去另一条路线.
有没有人用EF CF进行地理空间搜索?如果是这样,你是怎么做到的,为什么?
ef-code-first ×10
c# ×4
asp.net-mvc ×2
.net-core ×1
code-first ×1
data-access ×1
datetime ×1
mapping ×1
nullable ×1
one-to-many ×1
sql-server ×1