First, ASP.NET MVC code: Multiplicity is not acceptable in relationship roles.

asp.net c# entity-framework

Question

I am in a straightforward marriage. Two Pages are connected to a campaign. One campaign may only have one link to a page. But I keep encountering this mistake:

System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'Page_Campaign_Source' in relationship 'Page_Campaign'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

While comparing it to my code, I looked through numerous tutorials and sample codes, but I couldn't locate any errors.

 public class Campaign
    {
        [Key()]
        public int Campaignid { get; set; }
        public string Name { get; set; }

        public virtual Page LandingPage { get; set; }
        public virtual RedeemPage RedeemPage { get; set; }
    }


public class Page
{
    [Key()]
    public int PageContentId { get; set; }
    public string Logo { get; set; }
    public string Css { get; set; }

    [ForeignKey("Campaign")]
    public int campaignID { get; set; }
    public virtual Campaign Campaign { get; set; }
}

Edit

I utilized the Fluent API in instead of using Eranga's suggestion, but now I'm getting:

An error occurred while saving entities that do not expose foreign key properties for their relationships

1
3
10/9/2015 2:30:00 PM

Accepted Answer

In this circumstance, the Data Annotations Mapping is unclear. Configure Fluent via the API. Use fluent API as follows and remove the data annotations for navigational property mapping.

class MyContext : DbContext
{

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Campaign>().HasRequired(x => x.LandingPage)
           .WithMany();

        modelBuilder.Entity<Page>().HasRequired(x => x.Campaign)
           .WithMany()
           .HasForeignKey(x => x.campaignID); 

      base.OnModelCreating(modelBuilder);
    }
}

Edit

the issue after settingWillCascadeOnDelete(false) Is both that?Campaign and Page has automatically increasing PKs and a 1-to-1 mapping. As a result, in order to save one record, it requires both the ID of the first row and the ID of the second row that was just put.

You may either make 1 FK nullable and call to or alter the PK to be a GUID.SaveChanges two times.

The connection is now nullable.

    modelBuilder.Entity<Campaign>().HasOptional(x => x.LandingPage)
       .WithMany();


using(var scope = new TransactionScope())
{

       context.Campaigns.Add(campaign);
       context.SaveChanges();

       page.CampaignId = campaign.CampaignId;
       context.Pagess.Add(page);
       context.SaveChanges();

       scope.Complete();
}
2
3/27/2012 11:42:46 AM

Popular Answer

Instead of the two child entities, I believe your campaign should consist of a collection of pages.

I haven't programmed in MVC in a while, but if I remember properly, it should be something like:

public virtual Collection<Page> pages

Then you then insert your two pages into



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow