entity-framework-4

Generic repository implementation with EF

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 19:10:37
For a simple repository public interface ISimpleRepository<T> { IApplicationState AppState { get; set; } void Add(T instance); void Delete(T instance); void Delete(Guid rowGuid); IQueryable<T> GetAll(); T Load(Guid rowGuid); void SaveChanges(); void Update(T instance); } my implementation of the Load() method for specific repository for class Product might look like this: public Product Load(Guid rowid) { return (from c in _ctx.Products where c.id == rowid select c).FirstOrDefault(); } Now this is assumed when my repository implementation class looks like this: public class

Is there an Update Object holder on Entity Framework?

时光毁灭记忆、已成空白 提交于 2019-12-08 17:32:29
I'm currently inserting/updating fields like this (if there's a better way, please say so - we're always learning) public void UpdateChallengeAnswers(List<ChallengeAnswerInfo> model, Decimal field_id, Decimal loggedUserId) { JK_ChallengeAnswers o; foreach (ChallengeAnswerInfo a in model) { o = this.FindChallengeAnswerById(a.ChallengeAnswerId); if (o == null) o = new JK_ChallengeAnswers(); o.answer = FilterString(a.Answer); o.correct = a.Correct; o.link_text = ""; o.link_url = ""; o.position = FilterInt(a.Position); o.updated_user = loggedUserId; o.updated_date = DateTime.UtcNow; if (o

How do we alias a Sql Server instance name used in a Connection String .config?

£可爱£侵袭症+ 提交于 2019-12-08 16:55:28
问题 I have two development teams, that come from different groups. Group A develops against a local default instance of Sql Server 2008 R2; Group B develops against a local named instance of Sql Server 2008 R2. Is there a way to setup an alias such that both groups are coding against the same name? As it stands right now we have a war of Connection Strings as group B changes (local) to ./DEV and group A changes it back again? 回答1: In SQL Server Configuration Manager, under the SQL server native

.Net Framework 4.5 AddObject() does not appear

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 16:35:32
问题 I have a class that I want to make Insert , Update , Delete operations in it. // Constructor. public BaseManager() { // Disable lazy loading. this.Context.Configuration.LazyLoadingEnabled = false; } public DBEntities Context = new DBEntities(); In this class, I can't use AddObject() extension method on Context variable. AddObject() method does not appear typing after Context. Here are my imported namespaces: using System; using System.Collections.Generic; using System.Data.Linq; using System

Id of newly added Entity before SaveChanges()

十年热恋 提交于 2019-12-08 15:11:22
问题 I am adding an entity to my database like so: public TEntity Add(TEntity entity) { return (TEntity)_database.Set<TEntity>().Add(entity); } However, the DbSet.Add method isn't returning the newly added entity with the Id etc, it is simply returning the object I passed in to my Add method. Should it be passing me back the complete new entity including Id , or is it not possible to get the Id before SaveChanges() is called? 回答1: All the call to Add() is doing is effectively registering it with

What does StoreGeneratedPattern mean?

情到浓时终转凉″ 提交于 2019-12-08 14:45:46
问题 I'm doing EF design, who could tell me what does StoreGeneratedPattern mean? I can't find a easy straight answer online. 回答1: If you look at the samed called enumeration it tells what should be done if you insert or update rows: None: No auto generated value is generated Identity: A new value is generated on insert, but not changed on update Computed: A new value is generated on insert and update 回答2: These answers are also not an easy straight answer and just point to or repeat the same

Entity Framework 4: Adding dervied entity won't show in count until save

守給你的承諾、 提交于 2019-12-08 13:29:26
问题 I have an "Investment" entity that is derived from "BaseEntity". If I create and add a new "Investment" like so: investment = new Investment(); investment.Name = "Investment 01"; _context.BaseEntities.AddObject(entity); and query the count (before saving changes) _context.BaseEntities.OfType<Investment>().Count(); it returns zero. The same line returns 1 after _context.SaveChanges(); What am I doing wrong? 回答1: You are not doing anything wrong. This is how EF works. No changes are done in

Accessing HttpContext.Current in Data Access Layer

不羁岁月 提交于 2019-12-08 12:35:03
问题 Based on the given answer to my question at Entity Framework in layered architecture, Now I'd like to move my repositories (Which are now only responsible for CRUD abstraction, not business logic stuff) to DAL and reserve BLL for Business Logic. I've come to the conclusion that the entity context should be considered a unit-of-work and therefore not reused. So I'd like to create an obejctcontext per HttpContext in my repositories to prevent performance/thread [un]safety issues. I'd like to

entity framework - naming of model with same types

筅森魡賤 提交于 2019-12-08 11:55:42
问题 I have 2 tables: Bikes and Wheels. Bike has 2 FKs to Wheels: Bike.FrontWheelID and Bike.BackWheelID. EF maps these onto Bike.Wheel and Bike.Wheel1, just using their type for name, which I would prefer to avoid. Is there a way to change this that would survive further regenerations of model from db schema? (some setting perhaps?) Thanks 回答1: You can rename those properties in Entity Model Designer and it will not affect further regenerations of the model. 来源: https://stackoverflow.com

Entity Framework and differences between Contains between SQL and objects using ToLower

耗尽温柔 提交于 2019-12-08 11:46:25
问题 I have run into an "issue" I am not quite sure I understand with Entity Framework. I am using Entity Framework 4 and have tried to utilize a TDD approach. As a result, I recently implemented a search feature using a Repository pattern. For my test project, I am implementing my repository interface and have a set of "fake" object data I am using for test purposes. I ran into an issue trying to get the Contains clause to work for case invariant search. My code snippet for both my test and the