entity-framework

Create generic selector for Select() while using Entity Framework

半世苍凉 提交于 2021-02-10 13:25:10
问题 I would like to create a function to retrieve a List of a given property name's Type. But i dont know yet how to create a working lambda selector. public IList<object> GetDistinctListOfProperty(string propertyName) { var propInfoByName = typeof(T).GetProperty(propertyName); if (propInfoByName == null) return new List<object>(); using (var db = new ApplicationDbContext()) { var lambda = //TODO return db.Set<T>().Select(lambda).Distinct().ToList(); } } 回答1: You can use this method to generate

Entity Framework reference property

空扰寡人 提交于 2021-02-10 12:43:12
问题 I am working with Entity Framework 6. I am working with 2 classes: public partial class StateProvince { public StateProvince() { Addresses = new HashSet<Address>(); } public int StateProvinceID { get; set; } public string StateProvinceCode { get; set; } public string CountryRegionCode { get; set; } public bool IsOnlyStateProvinceFlag { get; set; } public string Name { get; set; } public int TerritoryID { get; set; } public Guid rowguid { get; set; } public DateTime ModifiedDate { get; set; }

Minimum database user permissions using Entity Framework database first approach

守給你的承諾、 提交于 2021-02-10 12:06:39
问题 What is the minimum set of database user permissions in SQL Server using Entity Framework database first approach in an ASP.NET MVC app? Is it only read and write or does EF need some more advanced permissions? 回答1: We typically use the following permissions Db Access admin, Db data reader And db data writer. These were sufficient in most cases. 来源: https://stackoverflow.com/questions/29052006/minimum-database-user-permissions-using-entity-framework-database-first-approach

EF 6, code first junction table name

我的未来我决定 提交于 2021-02-10 10:56:32
问题 I am experimenting with custom naming convenctions in EF 6. I have 2 tables and one junction table (WebUser, UserRequest, WebUserUserRequest). I have written function that should be able to rename tables: from WebUser to web_user private string GetTableName(Type type) { var result = Regex.Replace(type.Name, ".[A-Z]", m => m.Value[0] + "_" + m.Value[1]); return result.ToLower(); } It is applied this way: modelBuilder.Types() .Configure(c => c.ToTable(GetTableName(c.ClrType))); The function is

EF 6, code first junction table name

霸气de小男生 提交于 2021-02-10 10:52:07
问题 I am experimenting with custom naming convenctions in EF 6. I have 2 tables and one junction table (WebUser, UserRequest, WebUserUserRequest). I have written function that should be able to rename tables: from WebUser to web_user private string GetTableName(Type type) { var result = Regex.Replace(type.Name, ".[A-Z]", m => m.Value[0] + "_" + m.Value[1]); return result.ToLower(); } It is applied this way: modelBuilder.Types() .Configure(c => c.ToTable(GetTableName(c.ClrType))); The function is

Cross database queries.How to proper use cross database features?

萝らか妹 提交于 2021-02-10 09:42:21
问题 I am investigating the possibility to split one DB into multiple. We decided to move some tables into another database, but we have queries with join on these tables. I found a few solutions about how to achieve that: Azure SQL Database elastic query EXTERNAL DATA SOURCE But I don`t know what the difference between them and what to choose. Thanks for any help! 回答1: Azure SQL Database Elastic Queries and External data sources are two names for the same concept. My suggestion is to avoid cross

DbContext.SaveChangesAsync Exception Handling

不羁岁月 提交于 2021-02-10 07:26:03
问题 When scaffolding a new ApiController with asynchronous actions and Entity Framework support in Visual Studio 2013, some methods wrap DbContext.SaveChangesAsync calls in try-catch blocks. For instance, the Put method, try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return NotFound(); } throw; } From msdn.microsoft.com about DbUpdateConcurrencyException , Exception thrown by DbContext when it was expected that SaveChanges for an entity

DbContext.SaveChangesAsync Exception Handling

若如初见. 提交于 2021-02-10 07:23:10
问题 When scaffolding a new ApiController with asynchronous actions and Entity Framework support in Visual Studio 2013, some methods wrap DbContext.SaveChangesAsync calls in try-catch blocks. For instance, the Put method, try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return NotFound(); } throw; } From msdn.microsoft.com about DbUpdateConcurrencyException , Exception thrown by DbContext when it was expected that SaveChanges for an entity

Why is EF Core Update failing to update a modified column

只愿长相守 提交于 2021-02-10 06:42:06
问题 Im using EF Core and Im trying to update a column/property of an entity. The column has a foreign key constraint...is nullable and is an int. The same table/entity has three or four other columns/properties the same datatype also foreign key constrained...and nullable When I update the values of any of these column using the Update command...It works perfectly fine...EXCEPT for one column. When I try to update that one column and i process the update it will save all changes...but that column

EF Core model building conventions

风流意气都作罢 提交于 2021-02-10 06:27:09
问题 In EF6 it was possible to define conventions based on property types during model building, like so... public interface IEntity { Guid Id { get; } } public class MyEntity : IEntity { public Guid Id { get; set; } } public class MyDbContext : DbContext { public override void OnModelCreating(DbModelBuilder builder) { builder .Properties<Guid>() .Where(x => x.Name == nameof(IEntity.Id) .Configure(a=>a.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)); } } This approach could also be