Quantcast
Channel: entityframework Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1793

New Post: "Code First from Database" not picking up optional relationships

$
0
0
Hey Chris,

I'm not able to reproduce this issue. When I run the Code First from Database wizard it correctly configures the relationship. Here are the steps I used and perhaps you can let me know how they differ from what you are seeing.

The script included some references to other table (such as luLanguage) so I had to edit it a bit, here is what I ran:
CREATE TABLE [PersonLanguage](
    [PersonLanguageId] [bigint] IDENTITY(1,1) NOT NULL,
    [PersonId] [bigint] NOT NULL,
    [LanguageID] [tinyint] NOT NULL,
    [LanguageTypeId] [tinyint] NULL,
    [Dialect] [varchar](100) NULL,
    [CodeSetName] [varchar](100) NULL,
    [CodeHexValue] [varchar](64) NULL,
    [CodeHexValueDashed] [varchar](64) NULL,
    [StatusId] [smallint] NULL,
    [CreatedBy] [varchar](50) NOT NULL,
    [CreatedDate] [datetime] NOT NULL,
    [ModifiedBy] [varchar](50) NULL,
    [ModifiedDate] [datetime] NULL,
    CONSTRAINT [PK_PersonLanguage] PRIMARY KEY CLUSTERED 
    (
        [PersonLanguageId] ASC
    ))

CREATE TABLE [PersonLanguageOtherCode](
    [PersonLanguageId] [bigint] NOT NULL,
    [OtherCode] [varchar](50) NOT NULL,
    [CodeSetName] [varchar](100) NULL,
    [StatusId] [smallint] NOT NULL,
    [CreatedBy] [varchar](50) NOT NULL,
    [CreatedDate] [datetime] NOT NULL,
    [ModifiedBy] [varchar](50) NULL,
    [ModifiedDate] [datetime] NULL,
    CONSTRAINT [PK_PersonLanguageOtherCode] PRIMARY KEY CLUSTERED 
    (
        [PersonLanguageId] ASC
    ))


ALTER TABLE [PersonLanguageOtherCode]  WITH CHECK ADD  CONSTRAINT [FK_PersonLanguageOtherCode_PersonLanguage] FOREIGN KEY([PersonLanguageId])
REFERENCES [PersonLanguage] ([PersonLanguageId])
GO
ALTER TABLE [PersonLanguageOtherCode] CHECK CONSTRAINT [FK_PersonLanguageOtherCode_PersonLanguage]
GO
The classes are generated the same as what you originally posted. Because this is a relationship where the foreign key is also the primary key of the child table, it can't be configured via data annotations, so there is some Fluent API configuration included in the context that is generated.
public partial class LanguageContext : DbContext
{
    public LanguageContext()
        : base("name=LanguageContext")
    {
    }

    public virtual DbSet<PersonLanguage> PersonLanguages { get; set; }
    public virtual DbSet<PersonLanguageOtherCode> PersonLanguageOtherCodes { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // <code removed for brevity>

        modelBuilder.Entity<PersonLanguage>()
            .HasOptional(e => e.PersonLanguageOtherCode)
            .WithRequired(e => e.PersonLanguage);

        // <code removed for brevity>

    }
}
Could you just confirm that you have the latest version of the tooling installed? The easiest way to check is to go to Add or remove programs and search for Entity Framework. If you aren't using EF6.1.1, can you upgrade and see if the issue is resolved - http://www.microsoft.com/en-us/download/details.aspx?id=40762.

~Rowan

Viewing all articles
Browse latest Browse all 1793

Trending Articles