entity-framework-4.1

MVC3/EF4.1 Binding HttpPostedFileBase to model and adding to DB

爷,独闯天下 提交于 2019-12-11 18:22:18
问题 I have an ActionResult which binds data to the model and adds it the the DB. Now what I want, is to have a file uploader, and the ActionResult to store the files and adding their FileName to the DB (so I can show the files/images at a later point). What is the best approach? Here is what I got so far (It can store the files, but im not sure how intelligent EF is, and how complex the datatypes can be): The model class: public class Annonce { public int Id { get; set; } public string Company {

Add model with link to model object EFModelFirst

喜你入骨 提交于 2019-12-11 15:37:22
问题 So I have 2 models: Person and Ingredient and the thing I want to accomplish is that anyone can choose an ingredient (through a jQuery autocomplete), input their data and then on create it saves that to the database. So it would be like this: public class Person { public int PersonID { get; set; } [Required(ErrorMessage="Given name is a required field")] [DisplayName("Given name")] public string GivenName { get; set; } [Required(ErrorMessage="Family name is a required field")] [DisplayName(

How to make an Entity Framework property NOT NULL, but not required in form submission

吃可爱长大的小学妹 提交于 2019-12-11 13:48:31
问题 I'm using Entity Framework 4.1 with a code-first model. A common pattern is that many objects reference the user who owns them, eg. public class Item { public User Owner { get; set; } } This creates a nullable column in the DB, but since every Item must have an owner I want the column marked NOT NULL. If I use the [Required] attribute then submitting the form to create an Item results in an error. That field is never set through a form, only manually in code. 回答1: It is generally recommended

Entity framework use navigation properties on newly created entity

雨燕双飞 提交于 2019-12-11 13:41:09
问题 My business has a method which creates a MailMessage from an email. The method I use gets an Email object as a parameter, which is a simple POCO object, the foreign key properties like ToId and FromId are already set on it. The entity also have navigation properties to EmailAddress entities (FromEmailAddress and ToEmailAddress). What I want to achive is to use these navigation properties. The way I was able to this is the following, but it looks like a hax: public MailMessage CreateEmail

Entity Framework 4.0 Code-First Dynamic Query

蓝咒 提交于 2019-12-11 12:39:43
问题 I would like to query a table based on a list of KeyValuePair . With a Model-First approach, I could do the following: var context = new DataContext(); var whereClause = new StringBuilder(); var objectParameters = new List<ObjectParameter>(); foreach(KeyValuePair<string, object> pair in queryParameters) { if (whereClause.Length > 0) whereClause.Append(" AND "); whereClause.Append(string.Format("it.[{0}] = @{0}", pair.Key)); parameters.Add(new ObjectParameter(pair.Key, pair.Value)); } var

EF Many-to-many dbset.Include in DAL on GenericRepository

夙愿已清 提交于 2019-12-11 12:26:30
问题 I can't get the QueryObjectGraph to add INCLUDE child tables if my life depended on it...what am I missing? Stuck for third day on something that should be simple :-/ DAL: public abstract class RepositoryBase<T> where T : class { private MyLPL2Context dataContext; private readonly IDbSet<T> dbset; protected RepositoryBase(IDatabaseFactory databaseFactory) { DatabaseFactory = databaseFactory; dbset = DataContext.Set<T>(); DataContext.Configuration.LazyLoadingEnabled = true; } protected

Entity Framework 4.1 - how to update, insert and delete data in derived classes

烈酒焚心 提交于 2019-12-11 12:19:51
问题 I have a class called UserProfile, that derives from an object called User. How do I insert, update or delete data from the UserProfile? 回答1: You will define DbSet in your context. You can define set of base User type and it will be able to work with User and all derived entity types. public class Context : DbContext { public DbSet<User> Users { get; set; } } And using this is same as any other. Inserting: context.Users.Add(new UserProfile() { ... }); Modifying: var profile = GetSomeProfile()

Entity Framework 4.1 Code First with N-Tier saving double

隐身守侯 提交于 2019-12-11 12:11:54
问题 I have a WCF project with a entities with lots of children. I have a business layer and a data access layer, in the data access layer I have repositories that retrieve and save to my database. My understanding of EF is that you create and destroy the DataContext as and when you need it. As an example, lets say I have the a Person entity, and a Book entity (this is not my application, but just to try and illustrate the problem). Assume Person looks as follows. Person string Name vitual

Entity Framework Code 1st - Mapping many-to-many with extra info

那年仲夏 提交于 2019-12-11 11:22:41
问题 I've looked through several of the questions here and am not quite connecting all the (mental) dots on this. I would appreciate some help. My Models (code first): public class cgArmorial { [Key] [Display(Name = "Armorial ID")] public Guid ArmorialID { get; set; } [Display(Name = "User ID")] public Guid UserId { get; set; } public string Name { get; set; } public string DeviceUrl { get; set; } public string Blazon { get; set; } public virtual ICollection<cgArmorialAward> ArmorialAwards { get;

Code-First with existing empty database

安稳与你 提交于 2019-12-11 11:05:47
问题 I am doing a project, where I am going to use Entity Framework 4.1. I am planning on doing the "Code-First" approach, as I like clean entities and I want to extend them in some fashion. I have an existing empty database, that I would like to use. I have setup the connection string as below, and I have set EF to always drop and create database (to start with). And this is my problem. I have access to this empty database, but I don't have permissions to drop it and/or create it. The database is