entity-framework

Query with OData without exposing ORM models?

北城余情 提交于 2020-01-14 08:55:08
问题 In my web api 2 project, if I want to use OData library (which looks awesome and very tempting) for queries over some properties, that would force the client side to know the exact properties of my database models. Is this a good practice? Is there a way to avoid this decouple? For the following models: public class LetterEntity { public int Id {get; set;} public string Title {get; set;} public string Content {get; set;} public string Source {get; set;} public DateTime SendingTime {get; set;}

How do I perform date-part comparison in EF

霸气de小男生 提交于 2020-01-14 08:43:09
问题 i heard people saying date time comparison do not work just due to time part because datetime has time part. in sql i always compare datetime like this way and it works fine select * from employee where convert(varchar,dob,112) > '20111201' // this yyyymmdd format. how could i simulate this in a LINQ query? 回答1: The one thing to keep in mind is that operations on DateTime structs that represent database columns don't translate to SQL. So, you cannot write a query like: from e in

Cannot access a disposed object. A common cause of this error is disposing a context

耗尽温柔 提交于 2020-01-14 08:37:07
问题 I have written a simple application and when I navigate to my edit page the below error pops up. Microsoft.EntityFrameworkCore.Query[10100] An exception occurred while iterating over the results of a query for context type 'app.Models.ApplicationDbContext'. System.ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your

EF - Default value for new column with automatic migration

我的梦境 提交于 2020-01-14 08:03:46
问题 I use EF code first and automatic migrations. I want to add a new column to my model - a boolean column to present "active" (true) or "inactive" (false). How can I add this column and set a default value ("true") for the rows that already in the DB - with automatic migrations? 回答1: Tamar, you need set default value, see next sample: namespace MigrationsDemo.Migrations { using System; using System.Data.Entity.Migrations; public partial class AddPostClass : DbMigration { public override void Up

EF - Default value for new column with automatic migration

蹲街弑〆低调 提交于 2020-01-14 08:03:14
问题 I use EF code first and automatic migrations. I want to add a new column to my model - a boolean column to present "active" (true) or "inactive" (false). How can I add this column and set a default value ("true") for the rows that already in the DB - with automatic migrations? 回答1: Tamar, you need set default value, see next sample: namespace MigrationsDemo.Migrations { using System; using System.Data.Entity.Migrations; public partial class AddPostClass : DbMigration { public override void Up

Generic access to DbContext

两盒软妹~` 提交于 2020-01-14 08:02:26
问题 ObjectContext allows generic access to the generated Entities. DbContext appears to have no such support. Accessing EF5 with a generic repository is challenging. Let's say I want a generic mechanism to read any given entity, call it TEntity: public class DataRepositoryEF5<T> where T: DbContext { private ObjectContext _context; public DataRepositoryEF5(DbContext context) { _context = ((IObjectContextAdapter)context).ObjectContext; } public IEnumerable<TEntity> ReadAll<TEntity>() where TEntity

Entity Framework Not Generating Classes for Tables or Procedures

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-14 07:56:29
问题 I'm using the Entity Framework to generate the classes and functions in C# I need to interact with the SQL server. For reference, here's one of my tables: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Area]( [ID] [bigint] identity (1, 1) primary key, [Name] [nvarchar](100) NOT NULL ) GO After running the Entity Data Model Wizard (using 'EF Designer from database'), my project has the edmx file and a few new .cs files, but it seems like it's not generating everything it

Linq to Xml : Exception -The ' ' character, hexadecimal value 0x20, cannot be included in a name

回眸只為那壹抹淺笑 提交于 2020-01-14 07:17:06
问题 This is my Entity Class with an Entity : [Table(Name = "CLINICAL_ITEM_MASTER")] public class ClinicalItemMaster { [Column] public int CLIENT_INPUT_MHS_ID { get; set; } [Column] public Guid CLIENT_INPUT_MHS_GUID { get; set; } [Column] public string ITEM { get; set; } [Column] public int ITEM_ID { get; set; } [Column] public string ITEM_NUMBER { get; set; } [Column] public string CATEGORY { get; set; } [Column] public string DESCRIPTION { get; set; } [Column] public DateTime? CREATE_DTTM { get;

Linq to Xml : Exception -The ' ' character, hexadecimal value 0x20, cannot be included in a name

人走茶凉 提交于 2020-01-14 07:16:01
问题 This is my Entity Class with an Entity : [Table(Name = "CLINICAL_ITEM_MASTER")] public class ClinicalItemMaster { [Column] public int CLIENT_INPUT_MHS_ID { get; set; } [Column] public Guid CLIENT_INPUT_MHS_GUID { get; set; } [Column] public string ITEM { get; set; } [Column] public int ITEM_ID { get; set; } [Column] public string ITEM_NUMBER { get; set; } [Column] public string CATEGORY { get; set; } [Column] public string DESCRIPTION { get; set; } [Column] public DateTime? CREATE_DTTM { get;

Entity Framework 5 - Could not load file or assembly EntityFramework, Version=5.0.0.0

两盒软妹~` 提交于 2020-01-14 07:07:13
问题 I am trying to use Entity Framework 5 for my project but I seem to be having some issue getting the assembly installed to comply. And since I installed this initially using nuget, I am not certain what I need to do to cause this to work as I expect . Any help on what I need to do to fix this problem please? * System.IO.FileNotFoundException : Could not load file or assembly EntityFramework, Version=5.0.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089' or one of it's dependencies The