问题
Entity Framework v5 looks cool and I try to switch from Linq-to-SQL with existing MSSQL (Azure) database. but tutorials about EF are too complex to follow.
The database schema is pretty simple as follows (generated by exist db).
Sheets and SheetDetails tables are connected with 1-to-many relationship.
CREATE TABLE [dbo].[Sheets] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Grade] FLOAT (53) CONSTRAINT [DF_Sheets_Grade] DEFAULT ((0)) NOT NULL,
[Title] NVARCHAR (255) CONSTRAINT [DF_Sheets_Title] DEFAULT ('') NOT NULL,
[Description] NVARCHAR (255) NULL,
[Difficulty] SMALLINT CONSTRAINT [DF_Sheets_Difficulty] DEFAULT ((0)) NOT NULL,
[Writer] NVARCHAR (255) CONSTRAINT [DF_Sheets_Writer] DEFAULT ('') NOT NULL,
[Tag] NVARCHAR (255) NULL,
[Duration] FLOAT (53) CONSTRAINT [DF_Sheets_Duration] DEFAULT ((0)) NOT NULL,
[Timestamp] DATETIME CONSTRAINT [DF_Sheets_Timestamp] DEFAULT ((0)) NOT NULL,
[RowVersion] ROWVERSION NOT NULL,
CONSTRAINT [PK_Sheets] PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[SheetDetails] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Score] FLOAT (53) CONSTRAINT [DF_SheetDetails_Score] DEFAULT ((0)) NOT NULL,
[Number] SMALLINT CONSTRAINT [DF_SheetDetails_Number] DEFAULT ((0)) NOT NULL,
[SubNumer] SMALLINT NULL,
[IsRandom] BIT CONSTRAINT [DF_SheetDetails_IsRandom] DEFAULT ((0)) NOT NULL,
[AnswerType] SMALLINT CONSTRAINT [DF_SheetDetails_AnswerType] DEFAULT ((0)) NOT NULL,
[RowVersion] ROWVERSION NOT NULL,
[SheetDetail_Sheet] INT CONSTRAINT [DF_SheetDetails_SheetDetail_Sheet] DEFAULT ((0)) NOT NULL,
[SheetDetail_Question] INT CONSTRAINT [DF_SheetDetails_SheetDetail_Question] DEFAULT ((0)) NOT NULL,
CONSTRAINT [SheetDetail_Sheet] FOREIGN KEY ([SheetDetail_Sheet]) REFERENCES [dbo].[Sheets] ([Id]) ON DELETE CASCADE
);
Note that the FK name is SheetDetail_Sheet in SheetDetails table.
Result diagram is as below.
Next, maybe I need to write EntityTypeConfiguration. I tried it as question 1.
Question 1. I wrote model creating configuration as follows but no luck. is it wrong? hard to know how to write right configuration with this simple database model.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new SheetsConfiguration());
base.OnModelCreating(modelBuilder);
}
public class SheetsConfiguration : EntityTypeConfiguration<Sheets>
{
public SheetsConfiguration()
: base()
{
HasKey(p => p.Id);
Property(p => p.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
Property(p => p.RowVersion).IsConcurrencyToken().IsRowVersion();
HasOptional(p => p.SheetDetails).WithMany().Map(x => x.MapKey("SheetDetail_Sheet"));
ToTable("Sheets");
}
}
I execute simple query using
var result = _db.Sheets.Where(sheet => sheet.Id == id).ToList();
And then, I got an error "Invalid column name SheetDetail_Sheet" with executed query as follows.
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Grade] AS [Grade],
[Extent1].[Title] AS [Title],
[Extent1].[Description] AS [Description],
[Extent1].[Difficulty] AS [Difficulty],
[Extent1].[Writer] AS [Writer],
[Extent1].[Tag] AS [Tag],
[Extent1].[Duration] AS [Duration],
[Extent1].[Timestamp] AS [Timestamp],
[Extent1].[RowVersion] AS [RowVersion],
[Extent1].[SheetDetail_Sheet] AS [SheetDetail_Sheet]
FROM [dbo].[Sheets] AS [Extent1]
WHERE [Extent1].[Id] = @p__linq__0
I can understand because SheetDetail_Sheet is just an FK and not exist in the edmx properties and database column. How can I fix it?
I don't want to edit auto-generated model file because it can be overwritten. maybe it seems to be achieved with EntityTypeConfiguration.
Question 2. Is there any useful and lightweight reference with the commonly used master-detail database model?
I'm in trouble to start with existing database. stackoverflow, asp.net, blogs, etc...to many tutorials but hard to find an example like my case.
Thank you.
回答1:
I'd suggest using Model First to create your database model (though you're using database first). A good article for getting the database model is here. Just ignore the MVC stuff, and stop at "Generating Strongly Typed Entity Classes" as you are using EF5 which will generate the POCO classes for you. The Edmx should handle all of the FKs and properties for you and you won't need to worry about the configurator.
来源:https://stackoverflow.com/questions/13046614/select-error-on-ef-using-existing-master-detail-database