entity-framework

Create generic abstract class using EF database first approach

会有一股神秘感。 提交于 2020-01-17 07:15:10
问题 I have some similar entities and I want to create a generic abstract class for CRUD operations for them, but I don't know how can access to entities in generic class. For example: public abstract class HeaderRepository<T> { public HeaderRepository() { } public virtual byte[] Insert(T item) { using (MyEntities context = new MyEntities()) { context.***** <-- What I should write to add an object to entity T } } } I hope I've got it. 回答1: You need to set your DB set. It would be better to have a

Entity framework can't add with singleton

爷,独闯天下 提交于 2020-01-17 06:54:13
问题 My persons class: public class User { public int Id { get; private set; } public string Isim { get; set; } public string Soyad { get; set; } public User(string isim, string soyad) { Isim = isim; Soyad = soyad; } } My UserBusiness class: public sealed class UserBusiness { JuqueryDbEntities entity = new JuqueryDbEntities(); private static volatile UserBusiness instance; private static readonly object syncRoot = new Object(); private UserBusiness() { } public static UserBusiness Instance { get {

Entity framework , many changes missing after applying all migrations successfully,

给你一囗甜甜゛ 提交于 2020-01-17 06:38:08
问题 get-migrations shows full list of all my migrations but only half of them is in the _MigrationHistory and the database has changes from these half of them. No errors , all migrations applied successfully. issue comes while applying migrations having int to float changes were i had to manually add Sql("ALTER TABLE xxx DROP CONSTRAINT DF__TBL_OPRTN__OPRTN__6497E884"); . Is there any possible reason for these kind of behaviour from ef , any way to debug this? thanks 来源: https://stackoverflow.com

Invalid column name when trying to add an entity to a database using DbContext

痴心易碎 提交于 2020-01-17 05:53:11
问题 I'm trying to add an entity to my database with the following code: public void StoreElectronicSignatureType(ElectronicSignatureTypeModel model) { [...] ElectronicSignatureType electronicSignatureType = new ElectronicSignatureType(); Entity entity = GetEntity("Test"); electronicSignatureType.Entity = entity; Add(electronicSignatureType); public void Add(object entity) { DbContext.Entry(entity).State = System.Data.EntityState.Added; //DbContext.Set(entity.GetType()).Add (entity); DbContext

IQueryable being 3 times as slow with Contain-method vs Array

元气小坏坏 提交于 2020-01-17 05:46:47
问题 I have some records which I fetch from database (normally about 100-200). I also need to get the corresponding Place for every record and fill in the Description of the Place in the record. I would normally do that in my .Select function but I need to check if Place isn't null before trying to take the Description . My code goes like this: var places = db.Places.Where(p => p.Active && p.CustomerID == cust_ID).ToArray(); foreach (var result in query) result.Description = places.Where(Place.Q

EntitySqlException on select with code first dbcontext in Entity Framework

天涯浪子 提交于 2020-01-17 05:34:20
问题 When var codeFirstRepositor = new Donors(); var list = codeFirstRepositor.CampaignReps.Select(c => c).ToList(); When I put a breakpoint inside the CampaignRep class constructor it is not called at all - the exception seems to occur before object instantiation Context public class Donors : DbContext { public Donors() { // Needed for WCF serialization to work Configuration.ProxyCreationEnabled = false; } public DbSet<CampaignRep> CampaignReps { get; set; } } EntitySqlException 'CampaignsReps'

Views,Entity,Cannot deduce a primary key

浪子不回头ぞ 提交于 2020-01-17 05:14:31
问题 I made a view so the back hand of my website is more coherent. Then i tried to map it to my entity schema. I had the message : No primary key defined, could not deduce a primary key, table excluded So i made one ! I made sure it was something unique by combining 3 columns that cannot be identical when combined. And i called that new column "id". Entity dosen't seems to agree with the unique aspect of my "id" because the error message wont go away and i can't import my view... 回答1: Well, i had

Entity framework 4, update specific properties

血红的双手。 提交于 2020-01-17 05:11:06
问题 I'm used to using code first where I can do: db.Users.Attach(user); db.Entry(user).Property(propertyName).IsModified = true; How do I do this with a DataModel approach? I do not have the Entry method on my datacontext. 回答1: Use: context.Users.Attach(user); ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(user); entry.SetModifiedProperty(propertyName); 来源: https://stackoverflow.com/questions/6947950/entity-framework-4-update-specific-properties

Is changing the name of the EF __Migration History table dangerous?

房东的猫 提交于 2020-01-17 05:04:13
问题 Yesterday I asked this question about changing the name of the __Migration History table generated by Entity Framework when using a Code First approach. The provided link was helpful in saying how to do what we want (and by "want" I mean what we're being forced into by our DBAs), however also left a somewhat non-specific and dire-sounding warning that says, Words of precaution Changing the migration history table is powerful but you need to be careful to not overdo it. EF runtime currently

LINQ equivalent of string.Join that is usable in LINQ to Entities

泄露秘密 提交于 2020-01-17 05:01:27
问题 I have a metadata class with some custom properties for easier data access. One problem that I have run across though, is that when I try to use this property in LINQ statements I get this error. The specified type member 'CrewCodesStr' is not supported in LINQ to Entities. I know why I get it, I'm wondering if there is another way to do this to allow the property to be used in LINQ statements. Here is the property: public string CrewCodesStr { get { return string.Join(", ", CrewCodesList); }