entity-framework-6

EF6 AutoMapper6 Parent/Child different behavior

[亡魂溺海] 提交于 2019-12-02 08:58:19
问题 I just updated an entire WCF app from EF4 / AutoMapper 1.1 to EF6 / AutoMapper 6.0.0.2 and the behavior is not completely the same. This doesn't work for me : Entity Framework - Add Child Entity Before : child.Parent = parentObject OR parentObject.Children.Add(child) had the same result in real time (while debugging == before SaveChanges), so I decided to use child.Parent = parentObject for the readability. child.Parent = parentObject added a child in parentObject automatically. The child was

Is UPDATE command thread safe (tracking revisions) in MS SQL

梦想的初衷 提交于 2019-12-02 08:33:48
问题 Suppose we have a blog tool where each time a user performs a modification on an Article(Id,Body,Revisions) , the revision counter is incremented by 1. If we would execute the following query (in MS SQL), and, assuming that we have many people trying to update the article, would we then get the 'right' Revisions ? Since I'm using EF, I have expressed the query in the following way: context.Database.ExecuteSqlCommand("UPDATE dbo.Articles SET Revisions = Revisions + 1 WHERE Id=@p0;", articleId)

Entity Framework inserting duplicates on Seeding database [duplicate]

こ雲淡風輕ζ 提交于 2019-12-02 08:31:48
This question is an exact duplicate of: Entity Framework database mapping relationships (Duplicate creation using Seed() method) 1 answer EDIT---- From here i tried assigning Id's in the seeding method and this was OK for Languages but not when i added Address to the customer and assign Id's as well to these addresses, than it created the dupes again... Both Address & Language are declared as DbSet<...> in my Context What i tried: Adding 1 Address (with Id) - add this to 1 customer => Creates a dupe Adding 1 language & 1 Address (with Id's) - add both to 1 customer => Creates a dupe Adding 1

ASP Identity table column change but mapping is not possible with new column name

被刻印的时光 ゝ 提交于 2019-12-02 08:20:57
问题 I have changed the asp Identity so that in database Id column of AspNetIdentity table be UserId : modelBuilder.Entity<ApplicationUser>() .Property(p => p.Id) .HasColumnName("UserId"); And that works fine. Generated table have UserId instead Id . Now I have another table that should be mapped with AspNetUsers by UserId : When write it this way: public class JobApply { ... public int UserId { get; set; } public virtual ApplicationUser ApplicationUser { get; set; } Table in database looks like:

Determining the range of value for a field in database by using db-migration

人盡茶涼 提交于 2019-12-02 07:55:15
I've used Entity Framework 6.x and I've produced my database by code-first approach. After creating db, I've decided to some changes in my db. for example I want to determine a range of value for Size property in my model. my model: public class Tag : Entity, ITag { /// <summary> /// Size can be 1, 2, 3 or 4 /// </summary> [Range(1, 4)] public virtual int Size { get; set; } [Required] [StringLength(25)] public virtual string Title { get; set; } [StringLength(256)] public virtual string Description { get; set; } public virtual bool IsActive { get; set; } public virtual ISet<ArticleTag>

extend Where for IQueryable

你离开我真会死。 提交于 2019-12-02 07:39:15
I need to extend the Where method for IQueryable to something like this: .WhereEx("SomeProperty", "==", "value") I dont know if this is even possible, but i found this in SO : Unable to sort with property name in LINQ OrderBy I tried this answer and it seems quite interesting (Ziad's answer): using System.Linq; using System.Linq.Expressions; using System; namespace SomeNameSpace { public static class SomeExtensionClass { public static IQueryable<T> OrderByField<T>(this IQueryable<T> q, string SortField, bool Ascending) { var param = Expression.Parameter(typeof(T), "p"); var prop = Expression

How to link one-to-one relationship when fk is different from pk - EntityFramework

六月ゝ 毕业季﹏ 提交于 2019-12-02 07:16:29
I have a custom users table in my database and i want to create a one-to-one relationship with aspnetusers table so that when i register a new customer, applicationuser class through UserManager should add the username, email, password and school code to the users table and add an fk in its own table. IS there any tutorial/example implementing this kind of scenario? I am trying to add a one-to-one relationship with my users table but the fk is different than pk Users Table [columns] username password school code userId [pk] ASPNETUsers Table [standard columns] Id, [pk] UserName, PasswordHash,

Entity Data Model wizard disappears SQL Anywhere 17

我是研究僧i 提交于 2019-12-02 06:44:21
I am trying to make a new model using Entity Framework 6 and SQL Anywhere 17. I followed exactly these steps. http://dcx.sap.com/index.html#sqla170/en/html/37fb9e8558e94547b66156b9298be16f.html But when I go through Entity Data Model wizard, it disappears after the following screen. I got same Issue and able to resolve by following these step. Open server explorer tab- Delete all existing data connections- Rebuild your project- After this Entity Data Model wizard will not disappears. 来源: https://stackoverflow.com/questions/37178496/entity-data-model-wizard-disappears-sql-anywhere-17

Code First migration when entities have cross references with foreign keys

余生颓废 提交于 2019-12-02 05:41:01
问题 I have models that have references to each other: public class Dept { [Key] public int DeptId { get; set; } [ForeignKey("ManagerId")] public Emp Manager { get; set; } public int? ManagerId { get; set; } public string DeptName { get; set; } } public class Emp { [Key] public int EmpId { get; set; } [Required] [ForeignKey("DeptId")] public Dept Dept { get; set; } public int DeptId { get; set; } public string Name { get; set; } } When I call Add-Migration, I get error: The ForeignKeyAttribute on

EF: Create/Remove relation from Many-to-Many relations when `AutoDetectChangesEnabled` and `ProxyCreationEnabled` are disabled on DbContext

随声附和 提交于 2019-12-02 04:41:35
Knowning Foo.Id and Bar.Id how can I create their relation without loading the entities from the DB. class Foo { public int Id { get; set; } public Lst<Bar> Bars { get; set; } } class Bar { public int Id { get; set; } public Lst<Foo> Foos { get; set; } } Also this configuration are disabled in DbContext constructor: Configuration.AutoDetectChangesEnabled = false; Configuration.ProxyCreationEnabled = false; Configuration.LazyLoadingEnabled = false; And how it is possible to remove the relationship? Example: using (var ctx = new DbCtx()) { ctx.Configuration.LazyLoadingEnabled = false; ctx