entity-framework-6

Can you customise foreign key names in self referencing entities in entity framework?

假装没事ソ 提交于 2019-12-24 12:53:45
问题 Some of the entities in my application have 4 audit properties on them: public virtual DateTime WhenAdded { get; set; } public virtual DateTime? WhenUpdated { get; set; } public virtual User AddedBy { get; set; } public virtual User UpdatedBy { get; set; } I am using a code first approach and have the following extension method to map the user properties: public static void MapAuditFields<T>(this EntityTypeConfiguration<T> configuration) where T : class, IAuditable { configuration.HasOptional

How do I include a complex type to an Entity Index?

天涯浪子 提交于 2019-12-24 12:44:13
问题 I have these 2 POCOs... public class SqlTrace { public int id { get; set; } public string Name { get; set; } public virtual List<SqlTraceFile> TraceFiles { get; set; } } public class SqlTraceFile { public int id { get; set; } public string Name { get; set; } public virtual SqlTrace Trace { get; set; } } I created a 1 to many relationship between the trace and its files. I want to add an index that would make it so that SqlTraceFiles are unique to its SqlTrace; Allow multiple SqlTraceFiles to

Linq Expression to handle null

强颜欢笑 提交于 2019-12-24 12:26:35
问题 I am trying to use the following code in a linq expression, which I found at this question However it fails if the database field is null. public static IQueryable<T> FieldsAreEqualOrBothNullOrEmpty<T>( this IQueryable<T> source, Expression<Func<T, string>> member, string value) { Expression body; if (string.IsNullOrEmpty(value)) { body = Expression.Call(typeof(string), "IsNullOrEmpty", null, member.Body); } else { body = Expression.Equal( Expression.Call(member.Body, "ToLower", null),

Trying to using EF 6 with PostgreSQL - Failed to find or load the registered .Net Framework Data Provider

房东的猫 提交于 2019-12-24 12:09:06
问题 I am trying to use EF 6 Code First with Npgsql provider. When I try to run Add-Migration command in package manager I have the following exception: Failed to find or load the registered .Net Framework Data Provider. PM> Add-Migration -Force -ConfigurationTypeName "TestDbContextMigration" -ConnectionString "Server=localhost;Port=5432;Database=test_db;User Id=test;Password=konnaya;" -ConnectionProviderName "Npgsql" cmdlet Add-Migration at command pipeline position 1 Supply values for the

Entity Framework Code First model separation from domain

丶灬走出姿态 提交于 2019-12-24 11:59:24
问题 Entity Framework Code First best practice question? Hi All I am using EF codeFirst 6 on an NTier app. I have found that poco object that I am using to map to EF are really EntityFramework specific. Let me give you an example If I want to add a property that is not related to EF in the object ,EF does not like it. I Read you can put the "NotMapped" attribute however it start making this object difficult to maintain . Also there might be developers that are not familiar with EF and that will

C# Entity Framework recursive hierarchy query

馋奶兔 提交于 2019-12-24 11:46:05
问题 I'll first show my case in order to explain the question - I created a role and tasks architecture in SQL Server that looks like this: I have 2 main tables, Roles and Tasks , and 2 link tables. I have generated this model (using Entity Framework generator) to Entity Framework classes in C# and I got those classes: public class Task { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Role> Roles { get; set; } public virtual ICollection<Task> ChildTask {

Exception occurred while initializing the database. Login failed for user on re-creation after deletion

霸气de小男生 提交于 2019-12-24 11:22:10
问题 On the first run my program creates the database and log file correctly in the user's folder. If I then delete the database and log file and run the program again I get an error. using System; using blocks6.Module.BusinessObjects; namespace BlocksConsole { class Program { static void Main(string[] args) { try { AddToTable(); } catch (Exception e) { Console.WriteLine(e); throw; } } private static void AddToTable() { using (var db = new blocks6DbContext()) { var inf = new ModuleInfo(); db

Entity framework - NotSupportedException in lambda expression

六眼飞鱼酱① 提交于 2019-12-24 10:49:21
问题 In my MVC application ApplicationUser and Employee classes have 1-1 relationship: public class ApplicationUser : IdentityUser { public Employee Employee { get; set; } } public class Employee { [Key] public virtual ApplicationUser ApplicationUser { get; set; } public virtual string Name { get; set; } } In a public static class I have following methods: public static ApplicationUser GetCurrentApplicationUser(string userName) { using (DbContext db = new DbContext()) { return db.Users

How to update foreign key in EF 6 - Code First

倖福魔咒の 提交于 2019-12-24 10:12:35
问题 I'm trying to update a foreign key in EF6 (Code First) in an ASP.Net MVC manner. Let me explain : My entities public class Person { public int Id { get; set; } public string Name { get; set; } public virtual Country Country { get; set; } } public class Country { public int Id { get; set; } public string Name { get; set; } } My database Table Countries with 2 records : Id = 1, Name = France Id = 2, Name = Canada Table People with 1 record : Id = 1, Name = Nicolas, Country_Id = 1 My code // In

Entity framework 6 Lazy loading with db initiation

半腔热情 提交于 2019-12-24 09:35:29
问题 I am working with EF 6 and Lazy loading. class MainPrograme { static void Main(string[] args) { ProgramContext _dbContext = new ProgramContext(); _dbContext.Programs.Add(new Program { SecondProgram = new SecondProgram { Title = "Demo" } }); _dbContext.SaveChanges(); var item = _dbContext.Programs.Find(1); } } Once I disable lazy loading with Configuration.LazyLoadingEnabled = false; It works fine. No relational objects are loaded. item.SecondProgram is null . Perfect. However when I delete