有办法吗?以下不起作用:
var customer = constructor.Entity<Customer>();
customer.Property(c => c.Date).DataType("smalldatetime");
Run Code Online (Sandbox Code Playgroud) 首先是一些简要的背景:我使用实体框架V1现有的ASP.NET MVC 1个应用其中工程还算不错,但因为有越来越到40桌的的.edmx越来越笨重,容易出现腐败与Visual Studio 2008设计.我想要做的是看看将DAL迁移到使用EF4和Code-First是否可行.
最初我试图模拟简单的父/子关系,但没有达到很远.我有2个表Client和Address对应于以下POCO类:
public class Client
{
public int ClientId { get; set; }
public string Name { get; set; }
public Address HomeAddress { get; set; }
public Address WorkAddress { get; set; }
// More properties here
}
public class Address
{
public int AddressId { get; set; }
public string NameOrNumber { get; set; }
public string Line1 { get; set; }
// More properties here
}
Run Code Online (Sandbox Code Playgroud)
另外我有一个DbContext类,我使用流畅的api定义关系: …
我有一个遗留数据库有两个列,我想将它们映射为1 ID这是可能的
例如
public class Product
{
public string ProductID {get;set;}
public string ShortDescription {get;set;}
public string UserName {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
然后我的Modelbinder看起来像这样
modelBinder.Entity<Product>().
HasKey(p=>p.ProductID)
.MapSingle(product =>
new {
colShotDesc = product.ShortDescription,
colUser = product.UserName
}
).ToTable("Products");
Run Code Online (Sandbox Code Playgroud)
我需要的是像映射中的ProductID = ShortDescription + UserName ...因为这两个列共享一个独特的键约束...
不知道这是否有意义,但任何建议都会很棒...请不要问数据库设计=>这就像它是,不应该改变...这就是为什么我认为EF代码优先帮助我(希望交叉手指)...因为它看起来像db没有定义pk只是唯一的键约束...
无论如何......帮助会很棒..
我最近更新了一个使用LINQ to SQL和SQL Server CE 3.5到Entity Framework 4.1 Code First和SQL Server CE 4.0的应用程序,它现在运行速度明显变慢.在秒表测试之前和之后我做了一些,我的应用程序的大多数主要操作平均运行速度慢了约40%.
我正在使用EF Code First的所有默认策略和配置,但禁用级联删除除外.
当我最初发布这个问题时,我专注于一个似乎花了很长时间的查询,但我已经意识到它在第一次运行时只是特别慢(参见下面的评论帖子).
我现在认为我看到的是大多数查询运行速度较慢 - 速度不是很慢,但速度足够快,因为应用程序执行的大多数操作都涉及多个查询.
这个应用程序有一个非常小的数据库 SQL CE(.sdf)文件只有458 KB,最大的表少于250条记录.
这是一个POCO类示例:
public class Target
{
public int Id { get; set; }
public int TrialDefinitionId { get; set; }
public int Number { get; set; }
public int X { get; set; }
public int Y { get; set; }
public string Phase { get; set; }
public virtual TrialDefinition TrialDefinition { get; set; }
} …Run Code Online (Sandbox Code Playgroud) 我注意到,默认情况下,Entity Framework Code First忽略了实例化ICollection<T>属性,除非集合中至少有一个项目.我更倾向于保证集合总是空的HashSet(即,HashSet零项目),而不是null没有项目存在.
EF Code First是否有任何约定或设置可以启用此功能?
c# entity-framework code-first ef-code-first entity-framework-4.1
我正在尝试运行初始化代码,但它没有运行.这是我在main方法中的内容
static void Main(string[] args)
{
Database.SetInitializer<Context>(new RecipesSeedData());
}
Run Code Online (Sandbox Code Playgroud)
我是否应该在main中添加其他内容以使其运行以下代码?当我逐步调试调试器中的代码时,它甚至没有进入初始化代码,这让我觉得我错过了一些重要的东西.
public class RecipesSeedData : DropCreateDatabaseAlways<Context>
{
protected override void Seed(Context context)
{
var mt = new MenuType {MenuTypeId = 1};
context.MenuTypes.Add(mt);
base.Seed(context);
}
}
Run Code Online (Sandbox Code Playgroud) 在这个上再次撕掉我的头发...使用EF 5 Code First,在代码中构建模型 - 编译,所以它在语法上是正确的,但是当代码构建模型时我得到一个例外.这是实体类(我也有vaidation属性,但为了便于阅读,已将它们删除):
[Table("USERS")]
public class User : IPEntity
{
#region Constructor (needs to initialize list objects for related entities)
public User()
{
this.Profiles = new List<Profile>();
this.ProfileDivs = new List<ProfileDiv>();
this.ProfileDepts = new List<ProfileDept>();
}
#endregion
#region Entity properties and validation attributes
[Key]
public long UserId { get; set; }
public long PclientId { get; set; }
public string UserName { get; set; }
public string UserDescription { get; set; }
public long? EmpId { get; …Run Code Online (Sandbox Code Playgroud) 这是我关于堆栈溢出的第一个问题,所以我会尽量准确.这是我的模特:
namespace GRHMeca.Models
{
public class Projet
{
[Key]
public int ID { get; set; }
[Required]
public String Nom { get; set; }
public String Description { get; set; }
public List<Membre> Membres { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
而Membre是另一种常规模特.
我面临的问题如下:如果我这样做:
Projet projet = db.projets.Find(id);
IList<Membre> SelectionList = projet.Membres.ToList();
Run Code Online (Sandbox Code Playgroud)
我得到一个空指针异常; 但这很好用:
IEnumerable<Membre> SelectionList = new List<Membre>();
SelectionList = db.Membres
.ToList()
.AsEnumerable<Membre>()
.Except<Membre>(
db.Membres
.ToList()
.AsEnumerable<Membre>()
.Except<Membre>(
projet.Membres
.ToList()
.AsEnumerable<Membre>()
)
)
.ToList();
Run Code Online (Sandbox Code Playgroud)
我正在使用实体框架6,MVC 5和代码优先迁移.现在我有点担心性能,我想知道这个问题的原因以及如何避免它.
非常感谢
entity-framework object code-first complextype asp.net-mvc-5
我有这门课:
public class Comment
{
public virtual int Id { get; set; }
public string Body { get; set; }
public DateTime AddDate { get; set; }
[ForeignKey("PostId")]
public virtual Post Post { get; set; }
public int PostId { get; set; }
public virtual bool IsApproved { get; set; }
public virtual int LikeCount { get; set; }
public int? ParentId { get; set; }
public virtual Comment Parent { get; set; }
public ICollection<Comment> Children { get; set; …Run Code Online (Sandbox Code Playgroud) 我已经设置了我的EF代码优先数据库,但想要添加其他派生属性.(是的,它应该在视图模型中,我们可以讨论另一种时间为什么这样.)我创建了一个扩展实际表类的部分类.如果我添加一个[NotMapped]新的部分,它会避免映射我在那里添加的其他属性还是它将应用于整个类?