Tag: entity framework

.NET管理层关系

我实际上是在重新创建项目的默认架构。 在这篇文章中,我希望你不仅可以帮助我,还可以思考,讲述和改进现有方法。 这可能是非常有用的挑战。 此默认架构可以与所有人自由共享。 所以这里是描述:基本上MVC是我们的观点。 我们遵循OOP和层编程概念。 我们还将使用SQL Server和EF DB First 。 所以这就是我到目前为止所做的:我在我的解决方案中创建了4个层: 域:JUST域类 DAL:负责访问数据,包含UOW和存储库以及与数据访问相关的validation。 BL:独特负责业务逻辑和DAL的独特老板。 UI:这不是那么重要! (即使它可能是一个控制台应用程序!) 我已经使用通用函数实现BL,如getAll和getById以及crud funcs.these funcs将调用DAL通用函数,这也是准备好的。 现在一个重要的问题是:将DAL函数实现为GENERIC是否正确? 请考虑这种情况: 从UI我们发布模型到动作,在行动中我们称为BL Func( BL.insert (模型))。 BL.Insert函数会调用类似DAL.Add (模型)的东西(注意BL将调用DAL一次并告诉它它想要什么)。 现在DAL.Add func必须将3条记录插入3个不同的表中(模型从UI层传递)。 我认为这不可能通过DAL GENERICfunction来实现。 那么,在注意到关注点分离的情况下,实现图层和关系的正确和标准方法是什么? 在我的行动中我会: [HttpPost] public ActionResult Insert() { Bl.EntityBase.Article.Insert(new Article()); return RedirectToAction(“Index”); } 在BL我会: public void Insert(T obj) { Da.Repository().Insert(obj); } 在我的DAL中,我有: public virtual […]

种子实体和用户,角色?

您如何为用户,角色和应用程序特定实体设定种子? 似乎IdentityModel以其自己的Context为目标? internal sealed class Configuration : DbMigrationsConfiguration { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(Project.Models.SchoolContext context) { // Seed the Entities // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = “Andrew Peters” } // ); // } } 与 protected override void Seed(Project.Models.ApplicationDbContext context) { if (!context.Roles.Any(r […]

ADO.Netentity frameworkIEntityChangeTracker的多个实例不能引用实体对象

我正在尝试保存我的联系人,其中提到了ContactRelation(只是联系人,已婚,单身等的关系)和国家。 但每当我尝试保存我的联系人时,我都会得到例外“ADO.Netentity framework实例对象不能被IEntityChangeTracker的多个实例引用” public Contact CreateContact(Contact contact) { _entities.AddToContact(contact); //throws the exception _entities.SaveChanges(); return contact ; } 我正在使用松散耦合的MVC设计与服务和存储库。 我已经阅读了很多关于这个例外的post,但没有一个给我一个有效的答案…… 谢谢你,彼得

如何在Entity Framework中动态构造Order By Expression?

我使用以下方法构造Order By Expression 。 原始来源 它真的很光滑。 缺点是它只适用于Property是字符串类型。 如何在不为不同数据类型创建一堆方法的情况下使其接受不同的Property 类型 ? public static bool PropertyExists(string propertyName) { return typeof (T).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) != null; } public static Expression<Func> GetPropertyExpression(string propertyName) { if (typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) == null) { return null; } var paramterExpression = Expression.Parameter(typeof(T)); return (Expression<Func>)Expression.Lambda( Expression.PropertyOrField(paramterExpression, propertyName), paramterExpression); […]

entity framework大数据集,内存不足exception

我正在处理一个非常大的数据集,大约有200万条记录。 我有下面的代码,但在处理了三个批次,大约600,000条记录之后得到了一个内存不足的例外。 我理解,因为它循环遍历每个批处理entity framework的延迟加载,然后尝试将完整的200万条记录构建到内存中。 有没有办法卸载我处理过的批次? ModelContext dbContext = new ModelContext(); IEnumerable<IEnumerable> towns = dbContext.Towns.OrderBy(t => t.TownID).Batch(200000); foreach (var batch in towns) { SearchClient.Instance.IndexMany(batch, SearchClient.Instance.Settings.DefaultIndex, “Town”, new SimpleBulkParameters() { Refresh = false }); } 注意:Batch方法来自此项目: https : //code.google.com/p/morelinq/ 搜索客户端是这样的: https : //github.com/Mpdreamz/NEST

Fluent API,Entity Framework Core 2.0中的多对多

我使用EF Core 2.0,Code first和Fluent API搜索stackoverflow以获得生成多对多关系的正确解决方案。 一个简单的场景是: public class Person { public Person() { Clubs = new HashSet(); } public int PersonId { get; set; } public virtual ICollection Clubs { get; set; } } public class Club { public Club() { Persons = new HashSet(); } public int ClubId { get; set; } public virtual […]

entity framework:多对多关系中的重复记录

我有以下entity framework代码的第一个代码。 创建表并插入数据。 但是俱乐部表中有重复的记录。 我的业务是: – 使用俱乐部创建应用创建俱乐部 使用person app创建人员 如何避免重复输入? static void Main(string[] args) { Database.SetInitializer(new MyInitializer()); CreateClubs(); InsertPersons(); } public static void CreateClubs() { string connectionstring = “Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30”; using (var db = new NerdDinners(connectionstring)) { Club club1 = new Club(); club1.ClubName = “club1”; Club club2 = new Club(); club2.ClubName = […]

entity framework – 如何获取列?

我希望获得列名称,类型以及列是否是Entity Framework中表对象的PK的列表。 我如何在C#(4.0)(理想情况下)中执行此操作? 获胜的答案将是有效且最重要的一般答案。

EF6中的Eager,Lazy和显式加载

我已经阅读了本教程和本文,但我并不完全理解每种加载类型的用法。 我解释 我有这个POCO: public partial class dpc_gestion { public dpc_gestion() { this.ass_reunion_participant = new HashSet(); this.dpc_participant = new HashSet(); this.dpc_reunion = new HashSet(); } public int dpc_id_pk { get; set; } public Nullable dpc_id_gdp_fk { get; set; } public Nullable dpc_id_theme { get; set; } public int dpc_id_animateur_fk { get; set; } public Nullable dpc_date_creation […]

LINQ表达式语法如何与Include()一起用于预先加载

我在下面有一个查询,但我想对eager load属性执行Include()。 Actions有一个导航属性,User(Action.User) 1)我的基本查询: from a in Actions join u in Users on a.UserId equals u.UserId select a 2)第一次尝试: from a in Actions.Include(“User”) join u in Users on a.UserId equals u.UserId select a 但是没有填充Action.User。 3)尝试将’User’加载到查询之外的导航属性中: (from a in Actions join u in Users on a.UserId equals u.UserId select a).Include(“User”) 在LINQPad尝试包含的我得到一个错误: ‘System.Linq.IQueryable’不包含’Include’的定义,也没有扩展方法’Include’接受类型’System.Linq.IQueryable’的第一个参数可以找到(按F4添加using指令或程序集引用) 我认为这是因为LINQ不支持Include()。 所以我在VS中尝试过; 查询2运行,但返回未填充的用户属性。 查询3扩展方法似乎不存在,虽然它确实存在于Action本身而没有查询。