1
votes

Entity Framework code first (v6) creates a columnname in the database that I don't like. In tablename SharepointMappings it adds columnname: 'SharepointDestination_DestinationId' (foreign key). It also generates a columnname SharepointDestinationId. I would like to have 1 column, a foreign key, with the name 'SharepointDestinationId'.

My model looks like this:

public class Destination
{
    public int DestinationId { get; set; }
}

public class SharepointDestination : Destination
{
    public string UserName { get; set; }

    public string Password { get; set; }

    public string Domain { get; set; }

    public string SiteUrl { get; set; }

    public string DocumentLibraryName { get; set; }

    public List<SharepointMapping> Mappings { get; set; }
}

public class SharepointMapping
{
    public int SharepointMappingId { get; set; }

    public string SourceFieldName { get; set; }
    public string DestinationFieldName { get; set; }

    //[ForeignKey("SharepointDestination")]
    public int SharepointDestinationId { get; set; }

    //[ForeignKey("SharepointDestinationId")]
    public virtual SharepointDestination SharepointDestination { get; set; }
}
//.....
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // To use TPT inheritence
    modelBuilder.Entity<SharepointDestination>().ToTable("SharepointDestinations");

        //modelBuilder.Entity<SharepointMapping>()
        //    .HasRequired(m => m.SharepointDestination)
        //    .WithMany(d => d.Mappings)
        //    .HasForeignKey(m => m.SharepointDestinationId)
        //    .WillCascadeOnDelete(true);
}

It doesn't matter if i leave or add the attribute ForeignKey and it also doesn't matter if i make properties virtual or not. Completely deleting both properties on SharepointMapping or giving them a complete other name has no consequences. I think this has something to do with the inheritence structure. Because it's 'only' a 1-n mapping.

How should I configure EF to have only 1 column with the name 'SharepointDestinationId' which should be a foreign key? (and also have the navigation property and DestinationId property on the SharepointMapping class)

2

2 Answers

0
votes

Since the key of SharepointDestination is DestinationId, EF can't automatically figure it out. You could go with the annotation:

[ForeignKey("DestinationId")]
public virtual SharepointDestination SharepointDestination { get; set; }

and remove this:

[ForeignKey("SharepointDestination")]
public int SharepointDestinationId { get; set; }

The fluent should work as well if you comment out the annotation:

    modelBuilder.Entity<SharepointMapping>()
        .HasRequired(m => m.SharepointDestination)
        .WithMany(d => d.Mappings)
        .HasForeignKey(m => m.DestinationId)
        .WillCascadeOnDelete(true);
0
votes

The ForeignKey attribute is expecting a property name, not a table column name.

Really, you should be able to do this without any attributes.

The following should work:

public class Parent
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Child
{
    public int Id { get; set; }
    public string Name { get; set; } 

    public virtual Parent Parent { get; set; }
    public int ParentId { get; set; }
}