entity-framework

Entity Framework inserts row without me calling AddToObject()?

徘徊边缘 提交于 2020-01-17 04:37:07
问题 Okay, so it's late and I'm tired, but this shouldn't be happening... or so I believe. I have the following code for an event handler for a button click where I create a new customer from a web form. int customerId = <from_somewhere_doesnt_matter>; Customer cust; if (int.TryParse(Request.QueryString["cid"], out customerId)) { // update existing customer cus = db.Customers.Single(c => c.CustomerId == customerId); cus.UpdatedByUser = user; cus.Updated = DateTime.Now; } else { // create new

EntityFramework 4.3.1 Code First Navigation Property Mapping and Visibility

笑着哭i 提交于 2020-01-17 04:06:27
问题 This is a long introduction for a short question, sorry!! I'm working with EF 4.3.1 Code First and I've got the following model public class Action { protected Action() { } public virtual int ActionID { get; protected set; } [Required] [StringLength(DataValidationConstants.NameLength)] public virtual string Name {get; set;} [StringLength(DataValidationConstants.DescriptionLength)] public virtual string Description { get; set; } public virtual ICollection<Role> Roles { get; set; } public

Why don't CreateDatabaseIfNotExists create a relationship although db gets created

最后都变了- 提交于 2020-01-17 03:55:05
问题 I create a db from my edmx file like this: Database.SetInitializer(new CreateDatabaseIfNotExists<myEntities>()); And the db is created. However, there is one recursive relationship missing. It is in the edmx, but not in the db. Why does it fail to create a recursive relationship and how can I fix it? By recrusive relationship I mean a table that has a relationship with itself. In the myEntities class, one of the fileds is this: public DbSet<Product> Products { get; set; } And in an auto

Custom Membership provider & MVC 4

妖精的绣舞 提交于 2020-01-17 03:52:47
问题 Continue from this solution : How to configure Ninject for MVC4 & custom Membership provide? I declare public interface IUserRepository : IRepository<UserModel> { MembershipUser CreateUser(string username, ... , string providername = null); void Logout(); Boolean Login(string userName, string Password, bool persistCookie = false); bool RegisterUser(UserModel user); } And implement within UserRepository public class UserRepository : RepositoryBase<MyDbContext, UserModel>, IUserRepository {

Cannot read nullable datetime value from sqlite db using .net core and Entity Framework

↘锁芯ラ 提交于 2020-01-17 03:36:15
问题 I have a model of a game that has a nullable DateTime value: public class Game { [Key] public int Id { get; set;} public virtual ICollection<PlayerGames> Players { get; set;} public DateTime StartTime { get; set; } public DateTime? EndTime { get; set; } public int? WinnersTeam { get; set; } } public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public DbSet<Game> Games { get; set; } public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)

Returned object has null/empty ICollection unless it is accessed/inspected first

≯℡__Kan透↙ 提交于 2020-01-17 02:37:11
问题 Seeding deserialised JSON data in the Context and then attempting to return the DbSet. api/module returns all Modules with empty ICollections (null if I don't instantiate them), where as the Assessments happily return the virtual Module. Previous experience in MVC whereby I would have been accessing the object before it being sent to the view, so I haven't encountered this issue before. The commented out line: //Enumerable.ToList(ModuleItems.Include(mi => mi.Assessments)); Resolves the issue

Entity Framework join across two databases

白昼怎懂夜的黑 提交于 2020-01-17 01:42:30
问题 I have a WebAPI. I need to return a list of ActivityRegisters . We have a repository that does it like this: public IQueryable<ActivityRegister> GetRegisters(int activityID) { return ActivityDBContext.ActivityRegisters.Where(x => x.ActivityID == activityID x.IsActive == true).OrderBy(x => x.ActivityRegisterID).AsQueryable(); } However, there is a nullable column on the ActivityRegister table called roomID . There is a Rooms table but it is in a different database which we have a

CRUD operations with connected POCOs instead of disconnected POCOs in EF code first

こ雲淡風輕ζ 提交于 2020-01-16 19:30:10
问题 I have an classic 3 layer project, that has these layers: UI (WinForm Project) BLL DAL(using EF codefirst) My entities are disconnected POCO s and I used them to pass data between layers and used them to bind to the UI controls. I load a graph of some related entities to UI(e.g. Order with it's OrderLine s and Product of each OrderLine ), user may add some new entities, edit some others and delete some others entities in this graph, and then send this graph to DAL to persist this changes to

Entity Framework - Common Query

心已入冬 提交于 2020-01-16 19:18:30
问题 referencing at previous post (Entity Wrapper - Custom) I still have some difficult about generic query to retrieving common field. I've a simple interface with one field only. All my entities inheritance from my interface. Then I've a class encapsulating my objectContext typed. Well, now I need to execute a linq query to get my IQuerable object. The following snippet goes on error at building time: public IQueryable<T> GetQuery<T>() where T : IEntity { var query = GetObjectSetSomehow; /

Refresh ObjectContext or recreate it to reflect changes made to the database?

让人想犯罪 __ 提交于 2020-01-16 19:06:27
问题 I have a web service that exposes operations to and from a database. I'm using Entity Framework 4 for the DAL. I'm wondering what would be the best way to handle operations to and from the database. Would it be best not to keep alive an instance of the ObjectContext and instead create one for each operation done to the database? This seems to be a very bad way to handle this, but i can't think of another way to keep up with the changes made to the database. If i keep the same context alive,