entity-framework-5

Get Column DataType from Entity Framework Entity

ぐ巨炮叔叔 提交于 2019-12-01 09:06:01
Using Entity Framework 5, database first. Is it possible (at run time) to get the data type of the database column that an entity's property represents? The .net type would also work fine if that's easier. IEnumerable<DbEntityEntry> entities = context.ChangeTracker.Entries() .Where( e => e.State == EntityState.Added || e.State == EntityState.Modified); foreach (DbEntityEntry entity in entities) { foreach (string propertyName in entity.CurrentValues.PropertyNames) { //so I know the entity and the property name. Can I get the data type? } } Use reflection on the entity to get the property info.

Entity Framework - Database First without config

∥☆過路亽.° 提交于 2019-12-01 09:02:53
I'm developing a class library that deals with an exiting db using EF. I want to avoid the consumer of the class library (and .exe or a web site) to have in the *.config file the Entity connection string. I want the connection string set a run-time. How do I set the connection string with Database First approach? There is no constructor overload that takes a connection string and when I created one (in a separate partial class) I got an "UnintentionalCodeFirstException". I have reviewed already the following links: Is there a way to change connection string in database first? . Its about

Execute SQL Alter commands for every session with Entity Framework 5 talking to Oracle 11g

馋奶兔 提交于 2019-12-01 08:38:52
I have a requirement to execute some SQL commands at the start of every database session. I am using Entity Framework 5 via DbContext talking to a Oracle 11g database. I would like to execute: ALTER SESSION SET NLS_COMP=ANSI; ALTER SESSION SET NLS_SORT=BINARY_CI; at the start of an session creation to get case insensitive searching. How best could I go about this? I've put the commands in the constructor of dbContext, but have only simple unit test and it does appear to work. But unsure if this is right thing to do public partial class Entities : DbContext { public Entities() : base("name

Combine two EF Queries, Unable to cast object of type System.Data.Entity.Infrastructure.DbQuery to System.Collections.Generic.IEnumerable

会有一股神秘感。 提交于 2019-12-01 07:28:14
问题 I have two Entity Framework queries, each returning two columns, and i would like to concat or join the results of both queries for the reason of binding, I have tried the Concat method but it's throwing: Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[VB$AnonymousType_3`2[System.String,System.String]]' to type 'System.Collections.Generic.IEnumerable`1[VB$AnonymousType_2`2[System.String,System.String]] '. Here is my code: Dim r = (From PropertyDefinitions In

Get Column DataType from Entity Framework Entity

我们两清 提交于 2019-12-01 07:12:22
问题 Using Entity Framework 5, database first. Is it possible (at run time) to get the data type of the database column that an entity's property represents? The .net type would also work fine if that's easier. IEnumerable<DbEntityEntry> entities = context.ChangeTracker.Entries() .Where( e => e.State == EntityState.Added || e.State == EntityState.Modified); foreach (DbEntityEntry entity in entities) { foreach (string propertyName in entity.CurrentValues.PropertyNames) { //so I know the entity and

Entity Framework Automatic Migrations Existing Database

独自空忆成欢 提交于 2019-12-01 06:41:44
I am building an ASP.Net MVC4 web application with Entity Framework 5. I had to use an existing sql server database but also wanted to use Code First, so I followed this tutorial http://msdn.microsoft.com/en-us/data/jj200620.aspx My application uses automatic migrations with Entity Framework. The version of sql server I was using throughout the development phase was 2008, however, at the last minute I've been told the database needs to work on sql server 2005. I got the database I was using in sql server 2008 setup (exact tables, property names, relationships etc) on sql server 2005. However,

Seed Database at Application Start - ASP MVC 3 and EF

自古美人都是妖i 提交于 2019-12-01 06:14:48
For some reason I cannot get my application to seed the database with some test data when the application starts up. Order of execution: 1) Application_Start() in Global.asax - Database.SetInitializer<LocatorContext>(new DropCreateDatabaseAlways<LocatorContext>()); - new LocatorContext.DropCreateIfChangeInitializer() .InitializeDatabase(new LocatorContext()); 2) onModelCreating() in my DBContext class 3) Page is rendered and no data is inserted into the database Any thoughts as to why or how I can fix it would be much appreciated. My Global.asax.cs File //Global.asax.cs protected void

EntityState.Deleted does not work, Remove(entity) does?

走远了吗. 提交于 2019-12-01 06:06:16
I've been struggling with EF when trying to read records, then delete those records in the same transaction. I was initially using the EntityState.Deleted method, which would give an error: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be

How to avoid System.Data.Entity.Infrastructure.DbUpdateException

六眼飞鱼酱① 提交于 2019-12-01 05:51:19
I have this DbContext object which consists of - - Employee - CompanyAddress (PK: AddressFirstLine, City) Note: one Employee can have many CompanyAddress Records are added to CompanyAddress table only if some address doesn't exists in CompanyAddress table. If I have two DBContext objects from database say Snapshot1, Snapshot2. Say when both these snapshots were taken, there were no records in CompanyAddress table. When changes were made to Snapshot1 and saved - records are written to CompanyAddress table. When changes were made to Snapshot2 and saved using mydataContext.SaveChanges();

Entity framework code first convert between class boolean and column integer

廉价感情. 提交于 2019-12-01 05:32:15
问题 I am using Entity Framework 5 code first . My table has a column called Active and its datatype is of type int . The values that are stored in Active are 0 , 1 and null . I have a class that I need to map to this table. public class CommandExecutionServer : IEntity { public int Id { get; set; } public bool? IsActive { get; set; } } Here is my configuration file. I am trying to map my boolean property in my class to the integer field in the database. class CommandExecutionServerConfiguration :