entity-framework-6

How can I configure Entity Framework to automatically trim values retrieved for specific columns mapped to char(N) fields?

半城伤御伤魂 提交于 2019-12-28 03:37:45
问题 I'm working with a third-party database in which all text values are stored as char(n) . Some of these text values are primary keys, whereas others are just normal human-readable text. For the latter, I want retrieved values to be automatically trimmed. I know I can add Trim to all of my LINQ to Entities queries, but this is messy, unreliable and unmaintainable. I would like to somehow configure Entity Framework to automatically trim values retrieved from specific columns. However, I don't

EF Code-First One-to-one relationship: Multiplicity is not valid in Role * in relationship

爷,独闯天下 提交于 2019-12-27 11:04:54
问题 I'm attempting to do the following: public class class1 { public int Id {get;set;} [ForeignKey("Class2")] public int Class2Id {get;set;} public virtual Class2 Class2 {get;set;} } public class class2 { public int Id { get; set;} [Required] public virtual int Class1Id {get;set;} [Required] [ForeignKey("Class1Id")] public Class1 Class1 {get;set;} } However every time I try to migrate my database I get the following error: Class1_Class2_Target: : Multiplicity is not valid in Role 'Class2_Class1

Linq: Select Most Recent Record of Each Group

血红的双手。 提交于 2019-12-25 20:05:08
问题 I want to get the latest record of each group from a SQL Server table using Linq. Table Example: I want to get this result: My Linq query returns one record for each company, but it doesn't return the most recent ones: var query = from p in db.Payments where p.Status == false && DateTime.Compare(DateTime.Now, p.NextPaymentDate.Value) == 1 group p by p.CompanyID into op select op.OrderByDescending(nd => nd.NextPaymentDate.Value).FirstOrDefault(); What am i missing here? Why isn't the

Disable associations when a foreign key do not exist

只愿长相守 提交于 2019-12-25 16:54:42
问题 I've created an edmx for my database. In it Entity framework removed a table and instead created an association between two tables because it matches a column name with the primary key in the other table. I do not want that as there is no real association between those tables. How can I remove that association and get a class for the middle table instead? Example: SomeTable Id int pk MiddleTable SomeTableId int fk SomeCode int OtherTable SomeCode int pk It's the MiddleTable which do not get a

ef context + database schema + DDD BoundedContext

拥有回忆 提交于 2019-12-25 12:21:58
问题 I understand that at least EF 6 supports multiple DbContexts. This can be used to model BoundedContext. I did some google searches but could not find a definitive answer to this question. Is it advisable to use different db schemas for different DbContexts/BoundedContext? I know that ORMs abstract away the persistence mechanisms but I personally can see parallels between shemas and ddd/ef contexts. 回答1: It is a possibility. As with most architectural questions, the answer is: it depends. In

EF6 Lazy Loading not working for a navigation property

独自空忆成欢 提交于 2019-12-25 12:08:14
问题 I've got 2 new entities in an otherwise working app, I've trimmed unrelated properties (everything is marked virtual): QuoteOverrideRequest public virtual int RequestedById { get; set; } public virtual int QuoteId { get; set; } public virtual int StatusId { get; set; } //nav properties public virtual Customer RequestedBy { get; set; } public virtual Quote Quote { get; set; } public virtual QuoteOverrideRequestStatus Status { get; set; } All 3 navigation properties are mapped the same way.

C# Automapper 6.1.1 - Collection/List destination properties loses values Entity Framework 6

夙愿已清 提交于 2019-12-25 09:35:00
问题 I have a problem when mapping collections with Automapper 6 and I can't find a solution. In the updatedArticle object below I have the old Created and Updated values left after mapping since they do not exist on the view model. However the values for Created and Updated in Descriptions are lost. The values that do come in via the view model are all updated correctly. What am I doing wrong? Automapper also loses Entity Framework 6 mapping for Article since those values are lost as well.

Mocking DbContext in Entity Framework 6.1

早过忘川 提交于 2019-12-25 09:24:24
问题 I have found a number of examples that show (apparently) a clear working example of mocking DbContext with EF 6, however, none of them seem to work for me and I am not entirely sure why. This is my unit test code that sets up the mock; var mockData = new List<User> { new User { Email = "my@email.com", Id = 1 } }.AsQueryable(); var mockSet = new Mock<DbSet<User>>(); mockSet.As<IQueryable<User>>().Setup(m => m.Provider).Returns(mockData.Provider); mockSet.As<IQueryable<User>>().Setup(m => m

Cannot update Entity data model from database due to TNS error

萝らか妹 提交于 2019-12-25 09:16:04
问题 I'm trying to update my Entity data model from the database. Earlier today, I ran my app just fine and was able to access the database, as I have a tnsnames.ora file that I copied and pasted into my project folder. Then, when i was trying to open my edmx file, I was getting a message along the lines of: "The ADO.NET provider 'Oracle.ManagedDataAccess.Client' is either not registered in the machine or application config file, or could not be loaded" I noticed that my machine didn't have ODAC

setup relation for 1 to 1 with entity framework

↘锁芯ラ 提交于 2019-12-25 08:56:34
问题 How do I have to setup the modelbuilder for the ContactId? A CountryCompanyAssignment has a relation to a Contact. When I delete the CountryCompanyAssignment I do not want that the contact is lost. When I create a CountryCompanyAssignment then I also need a contact to save the CountryCompanyAssignment. I need to setup the relation between CountryCompanyAssignment and the ContactId because when I delete the CountryCompanyAssignment I get an error saying I should add a foreign key for the