Joe
Joe

Reputation: 5497

Entity Framework Required With Optional

I have the following model where I am attempting to make the Notification property on a Request object be null or the id of a notification.

However, I am not quite sure of how to map this with the fluent mapping. HasOptional -> WithMany seems to be the closest I can get, but I'd like to ensure that the NotificationId column in Requests is unique. What is the best way to accomplish this with fluent mapping?

public class Request
{
    public int RequestId { get; set; }
    public string Description { get; set; }
    public int? NotificationId { get; set; }
    public virtual Notification Notification { get; set; }
}

public class Notification
{
    public int NotificationId { get; set; }
    public string Description { get; set; }
    public DateTime CreateDate { get; set; }
}

public class RequestMap : EntityTypeConfiguration<Request>
{
    public RequestMap()
    {
        HasKey(x => x.RequestId);
        Property(x => x.Description).IsRequired().HasMaxLength(255);

        HasOptional(x => x.Notification)
            .WithWhat?
    }
}

Upvotes: 1

Views: 66

Answers (1)

Ahmed Ghoniem
Ahmed Ghoniem

Reputation: 681

using HasOptional(x => x.Notification) is enough you don't need WithMany

you dont have many Request with the same Notification

public class Request
{
    public int RequestID { get; set; }
    public string Description { get; set; }
    public int? NotificationId { get; set; }
    public Notification Notification { get; set; }
}

public class Notification
{
    public int NotificationId { get; set; }
    public string Description { get; set; }
    public DateTime CreateDate { get; set; }
}

public class RequestMap : EntityTypeConfiguration<Request>
{
    public RequestMap()
    {
        HasKey(x => x.RequestID);
        Property(x => x.Description).IsRequired().HasMaxLength(255);
        HasOptional(x => x.Notification);
    }
}

and the generated migration

public partial class initial : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Notifications",
            c => new
                {
                    NotificationId = c.Int(nullable: false, identity: true),
                    Description = c.String(),
                    CreateDate = c.DateTime(nullable: false),
                })
            .PrimaryKey(t => t.NotificationId);

        CreateTable(
            "dbo.Requests",
            c => new
                {
                    RequestID = c.Int(nullable: false, identity: true),
                    Description = c.String(nullable: false, maxLength: 255),
                    NotificationId = c.Int(),
                })
            .PrimaryKey(t => t.RequestID)
            .ForeignKey("dbo.Notifications", t => t.NotificationId)
            .Index(t => t.NotificationId);

    }       
    public override void Down()
    {
        DropForeignKey("dbo.Requests", "NotificationId", "dbo.Notifications");
        DropIndex("dbo.Requests", new[] { "NotificationId" });
        DropTable("dbo.Requests");
        DropTable("dbo.Notifications");
    }
}

Upvotes: 1

Related Questions