entity-framework-4.1

How to get the Primary Key(s) in Entity Framework 4.1, i.e. using DbContext

前提是你 提交于 2019-11-30 13:00:22
问题 The answers I'm seeing here are for ObjectContext. Is there a property to determine an entity's primary key names when using DbContext? Ah.. one of those times that I wish Entity Framework is open source! I can glean this primary key name information from .Find method :-) 回答1: You cannot use DbContext for that - DbContext API is just dumb wrapper with only most needed functionality. For everything more complex you must convert DbContext back to ObjectContext and use it. Try something like

Creating BiDirectional One - One relationship in Entity Framework 4.1 Code First

你离开我真会死。 提交于 2019-11-30 12:33:40
问题 I want to created Bi-Directional One-One relationship between two entities using EF Code First. I have trouble with the following code. What do you think I should do? public class User { public string ID { get; set; } public string LastName { get; set; } public string Password { get; set; } public string FirstName { get; set; } public int ProfileID { get; set; } public Profile Profile { get; set; } } public class Profile { public int UserID { get; set; } public User User { get; set; } public

Conflicting changes to the role x of the relationship y have been detected

对着背影说爱祢 提交于 2019-11-30 12:26:50
问题 I am having the exception Conflicting changes to the role x of the relationship y have been detected. Every time I add my entity to my context Database.MyEntitys.Add(MyEntity); The class MyEntity contain this property : public virtual ICollection<DetailInfo> Group { get; set; } The DetailInfo class is pretty simple: public class DetailInfo:BaseEntity { public virtual Detail Detail { get; set; } public decimal Total { get; set; } public virtual MyEntity MyEntity { get; set; } } The

How do I upsert a record in ADO.NET EF 4.1?

爷,独闯天下 提交于 2019-11-30 11:56:19
I'm trying to accomplish something really simple and I can't find how to do it using Entity Framework 4.1. I want a controller method that accepts an object and then does an UPSERT (either an insert or update depending on whether the record exists in the database). I am using a natural key, so there's no way for me to look at my POCO and tell if it's new or not. This is how I am doing it, and it seems wrong to me: [HttpPost] public JsonResult SaveMyEntity(MyEntity entity) { MyContainer db = new MyContainer(); // DbContext if (ModelState.IsValid) { var existing = db.MyEntitys.Find(entity.MyKey)

Should a Repository return IEnumerable<T> , IQueryable<T> or List<T>?

好久不见. 提交于 2019-11-30 11:38:30
I'd like to make my application as flexible as possible, but not dig myself into a hole by making my Interface too specific. What is the best object type for a repository? IEnumerable, IQueryable, or List? The technologies I'm considering using are Azure App Fabric Caching Entity Framework 4.1 Possibly Windows Server AppFabric I would say build your DAL using IQueryable, and pass it around, make sure your object contects lifetime is the request. This way you will get benefit of delayed execution, but are exposed to the risk of inefficient querying of database. Then make sure you performance

EF 4.1 Code First - map enum wrapper as complex type

廉价感情. 提交于 2019-11-30 11:22:58
问题 I'm trying to build a generic solution to the problem of enums with EF 4.1. My solution is basically a generic version of How to fake enums in ef 4. The enum wrapper class works wonderfully in the rest of the code and allows code like: EnumWrapper<Color> c = Color.Red; Here's the enum wrapper class: public class EnumWrapper<TEnum> where TEnum : struct, IConvertible { public EnumWrapper() { if (!typeof(TEnum).IsEnum) throw new ArgumentException("Not an enum"); } public TEnum Enum { get; set; }

Linq to Entities (EF 4.1): How to do a SQL LIKE with a wildcard in the middle ( '%term%term%')?

徘徊边缘 提交于 2019-11-30 11:09:42
I want to search for this: Post Cereal and get this: Post Honey Nut Cereal where the wild cards would be the spaces. I know I could do a SPLIT and a series of ANDs and Contains() and translation to a Linq Expression for each term as a specification object, but isn't there a way to honor wildcards in the term sent to SQL? I looked at SQL functions where it's in Linq to SQL, but I am not sure what it is in Linq to Entities. I would like to do something like this: term = '%' + term.Replace(' ', '%') + '%'; db.table.where( p => System.Data.Objects.SqlClient.SqlFunctions .SqlMethods.Like(p

Entity Framework Code-First - Define the key for this EntityType

别来无恙 提交于 2019-11-30 10:39:34
问题 Hi I'm planning to test EF Code First in one of my project. This is what I want actually. I have three tables and the structure is as follows public partial class App_user { public int id { get; set; } public string name { get; set; } public string email_address { get; set; } public string password { get; set; } public int user_type { get; set; } public List<Role> Roles { get; set; } } public partial class Role { public int id { get; set; } public string name { get; set; } } public partial

How do I allow the EF4 CodeFirst database initializer to run under development, but not run in production

人走茶凉 提交于 2019-11-30 10:23:01
I am attempting to deploy my first alpha version of a system online for a few people to start using. On development I make heavy use of the DropCreateDatabaseOnModelChange<TContext> (I don't have it in front of me at the moment so I can't verify the exact name) to re-initialize my dev database every time my model changes. This happens in the Global.asax . However, I do not want this to happen on my web host where other people are entering real data. I need to handle all db migrations on there myself so data is preserved. I had considered #ifdef DEBUG tags to prevent the database initializer

How to call Scalar-valued function from LINQ to Entities server-side

﹥>﹥吖頭↗ 提交于 2019-11-30 09:14:30
I have a Scalar-valued function in my DB: ALTER FUNCTION [dbo].[fx_fooFunct] (@MyParam varchar(max)) RETURNS varchar(max) AS BEGIN return @MyParam END I want to call this function from a LINQ to Entities query and get the result into a variable: let result = this.ObjectContext.ExecuteFunction<string>("SELECT dbo.fx_fooFunct(@MyParam)", new ObjectParameter("MyParam", "hello world")).FirstOrDefault() But, when I execute the code, I get this error: LINQ to Entities does not recognize the method 'System.Data.Objects.ObjectResult`1[System.String] ExecuteFunction[String](System.String, System.Data