entity-framework

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

萝らか妹 提交于 2020-01-16 19:06:25
问题 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,

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

谁说我不能喝 提交于 2020-01-16 19:06:05
问题 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,

Retrieve Role for a User in the DAL using ASP.NET Identity

不问归期 提交于 2020-01-16 18:44:11
问题 I have a bog-standard implementation of Users and Roles using ASP.NET Identity: public class User : IdentityUserBase { } public class Role : IdentityRoleBase { } public class UserRole : IdentityUserRoleBase { } I need a property or method in the User class that returns the (first) role of the user (in my app, each user can only have one): public class User : IdentityUserBase { // ... public Role GetRole() { return this.Roles.FirstOrDefault(); // this actually returns a UserRole object, with

Foreign keys in Entity Framework

Deadly 提交于 2020-01-16 18:19:52
问题 I have a method for inserting in database. I have 1:n relationship between the tables Point (n) and Line (1). Point has foreign key idLine . However, the class Point in Entity Framework doesn't have the idLine property. Now, in my method I have object of type Point , as parameter and here I have a problem because I don't know to what to assign the idSection of the new Point object which is inserted in the table. How to add the idLine property in the Point class? 回答1: I'll assume you're using

Exclude columns getting data with Entity Framework [duplicate]

空扰寡人 提交于 2020-01-16 18:16:06
问题 This question already has answers here : Exclude a field/property from the database with Entity Framework 4 & Code-First (4 answers) Closed 2 years ago . I'm using Entity Framework in C#. And I want exclude some columns while getting data from database. For example, my student table has 10 colums. And there is some columns in table like CreatedDate, CreatedBy, CreateRole, UpdatedDate, Updatedby, UpdatedRole. I want generic solution for exclude this column while getting list from database like

EF Code First to create multiple databases dynamically

喜你入骨 提交于 2020-01-16 18:06:08
问题 Is it possible to generate different databases according to a specific parameter? My final goal is john.domain.com => create john db, paul.domain.com => create paul db How could I achieve this using EF6 code first, MVC5? Could model first do it? 回答1: Yes you can change the connection string at runtime, something like. Need to add reference to System.Data . public static class ConnectionStringExtension { public static void ChangeDatabaseTo(this DbContext db, string newDatabaseName) { var

Entity Framework 4.1 and OriginalValues/CurrentValues and DbContext

橙三吉。 提交于 2020-01-16 15:22:31
问题 I'm currently using DbContext with Ef 4.1, and I'm trying to audit all changes some of my entities. I can capture the original and current values for any Properties of an entity, however I cannot figure out how to capture the association (Foreign Key) OriginalValues of a NavigationProperty. Has anyone figured this out? 回答1: You must either include foreign keys to your entities so they will be tracked as normal values or you must convert your DbContext to ObjectContext and use more powerful

EF 4 Query - Issue with Multiple Parameters

ⅰ亾dé卋堺 提交于 2020-01-16 12:30:13
问题 A trick to avoiding filtering by nullable parameters in SQL was something like the following: select * from customers where (@CustomerName is null or CustomerName = @CustomerName) This worked well for me in LINQ to SQL: string customerName = "XYZ"; var results = (from c in ctx.Customers where (customerName == null || (customerName != null && c.CustomerName == customerName)) select c); But that above query, when in ADO.NET EF, doesn't work for me; it should filter by customer name because it

How to use entity framework when Entities/Properties are only known at runtime?

[亡魂溺海] 提交于 2020-01-16 12:03:38
问题 I am using a mapping file that contains the Entity and the Column Names to perform the insert on so can this be done with entity framework ? The alternative version is just to use inline SQL and write code like sql = INSERT INTO " + someTable + etc.... 回答1: I don't think using Entity Framework (EF) will be a good fit for this project you're working on. One of the main benefits of using EF is the object graph; it takes care of recognizing new records, updated records and so on. If you are

EF Core creates multiple foreign key columns

跟風遠走 提交于 2020-01-16 10:44:13
问题 Im using EF Core with .Net Core 3.1 I have simple example of Client-Event relationship: public class BaseEntity { [Key] [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public DateTime CreatedOn { get; set; } public DateTime? ModifiedOn { get; set; } } public class Client : BaseEntity { public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public string Phone { get; set; } } public class Event :