ef-code-first

Entity Framework Code First: Configuration.cs seed or custom initializer

时光毁灭记忆、已成空白 提交于 2021-02-06 20:45:35
问题 I am working with the the Code First style of the Entity Framework for my first time. I want to set up some default data. The first approach I came across involved creating a custom initializer. I was headed this route but noticed after setting up migrations that it came with the Configuration.cs that already overrides the seed method just like the custom initializer. internal sealed class Configuration : DbMigrationsConfiguration<Toolkit.Model.ToolkitContext> { public Configuration() {

Table Valued Function and Entity Framework

时光毁灭记忆、已成空白 提交于 2021-02-06 08:47:48
问题 I'm trying to execute an TVF with Entity Framework and for some reason it just doesn't work. Maybe anyone out there can help me see the problem. Here are the code samples: That's the function: CREATE FUNCTION [dbo].[udf_profileSearch] (@keywords NVARCHAR(3000)) RETURNS @results TABLE ( [Id] [int] NULL, [SubCategoryId] [int] NULL, [UserId] [int] NULL, [SmallDescription] [nvarchar](250) NULL, [DetailedDescription] [nvarchar](500) NULL, [Graduation] [nvarchar](140) NULL, [Experience] [nvarchar]

Table Valued Function and Entity Framework

只谈情不闲聊 提交于 2021-02-06 08:47:18
问题 I'm trying to execute an TVF with Entity Framework and for some reason it just doesn't work. Maybe anyone out there can help me see the problem. Here are the code samples: That's the function: CREATE FUNCTION [dbo].[udf_profileSearch] (@keywords NVARCHAR(3000)) RETURNS @results TABLE ( [Id] [int] NULL, [SubCategoryId] [int] NULL, [UserId] [int] NULL, [SmallDescription] [nvarchar](250) NULL, [DetailedDescription] [nvarchar](500) NULL, [Graduation] [nvarchar](140) NULL, [Experience] [nvarchar]

Only 1 of Multiple FK relationships Constraint

有些话、适合烂在心里 提交于 2021-02-05 08:49:06
问题 I have a logical table structure where Table A has relationship to multiple other tables ( Table B & Table C ). But functionally Table A can only ever have the FK for B or C` populated. Valid Table A Record: |------|--------|---------|---------| | ID | Name | FK_B | FK_C | |------|--------|---------|---------| | 1 | Record | -null- | 3 | |------|--------|---------|---------| Invalid Table A Record: |------|--------|---------|---------| | ID | Name | FK_B | FK_C | |------|--------|---------|--

Addresses database schema for multiple entities

ⅰ亾dé卋堺 提交于 2021-01-29 19:53:22
问题 I'm playing around with entity framework and code first approach. The scenario is this: An user can have multiple companies (each company has an address) An user can have multiple houses per company (each house has an address) I'm thinking of two ways I can manage addresses: Have an Address table with a column for CompanyId and HouseId (for companies addresses only CompanyId will be inserted and for houses both Ids will be inserted. Have a CompanyAddress and HouseAddress tables with the only

How to Create Migration on Runtime in Entity Framework Core

送分小仙女□ 提交于 2021-01-29 16:07:08
问题 Is there any possible way to create migration on runtime in efcore. context.Database.Migrate(); Before this code, I need a code that, when I started my app, should create a migration about diff between postgresql database tables and ef models. Is there any way to do this? PM> enable-migrations PM> add-migration initial PM> update-database I dont want to use these ones. I want to make these code's jobs on runtime. I hope I could explain myself clearly. 回答1: Note, you need to re-compile the app

How to configure a foreign key on an owned EF entity's property?

不打扰是莪最后的温柔 提交于 2021-01-29 12:54:31
问题 EF Core 3.0 I have the following (simplified) entities in my domain: class ApplicationUser { public int Id { get; set; } public string UserName { get; set; } // other properties } [Owned] class Stamp { public string Username { get; set; } public ApplicationUser User { get; set; } DateTime DateTime { get; set; } } class Activity { public Stamp Created { get; set; } public Stamp Modified { get; set; } // other properties } It's not particularly relevant, but it's worth mentioning that

How to apply Entity Framework code first approach to a project

陌路散爱 提交于 2021-01-28 19:54:47
问题 I use Visual Studio 2013 and need to use Entity Framework code first approach in a project. I used NuGet Manager to install EF v6.1 into my project. But I can't create an empty code first model or even a code first model from database although there are two other choices (DB first and Model first). How I can add Entity Framework code first feature to my project? 回答1: Here is a beginners tutorial for using entity framework code first: An Absolute Beginner's Tutorial for understanding Entity

The column name is not valid

喜欢而已 提交于 2021-01-27 13:42:59
问题 I get a common error that probably is easy to solve but I have no clue how. This is the message I get. The column name is not valid. [ Node name (if any) = Extent1,Column name = Blog_BlogId ] Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlServerCe.SqlCeException: The column name is not valid. [ Node name (if

One to many recursive relationship with Code First

左心房为你撑大大i 提交于 2021-01-27 04:27:52
问题 I am trying to implement a simple self referencing relationship with EF 6.1.2 Code First. public class Branch { [Key] public int Id { get; set; } [Required] public string Name { get; set; } public int? ParentId { get; set; } [ForeignKey("ParentId")] public virtual Branch Parent { get; set; } public ICollection<Branch> Children { get; set; } // direct successors } In my application I have exactly one root branch. And except for this single root branch, every branch has exactly one parent (the