Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8891221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:41:05+00:00 2026-06-14T22:41:05+00:00

I have this model : public class FileUpload { public int FileUploadId { get;

  • 0

I have this model :

    public class FileUpload
{
    public int FileUploadId { get; set; }
    public string FileName { get; set; }

}

public class Company
{
    public int CompanyId { get; set; }
    public string Name { get; set; }

    public FileUpload Logo { get; set; }
    public int? LogoId { get; set; }

    public FileUpload Catalog { get; set; }
    public int? CatalogId { get; set; }
}

public class Ads
{
    public int AdsId { get; set; }
    public string Name { get; set; }

    public FileUpload Picture { get; set; }
    public int? PictureId { get; set; }
}

public class TestContext : DbContext
{
    public DbSet<Company> Companies { get; set; }
    public DbSet<Ads> Adses { get; set; }
    public DbSet<FileUpload> FileUploads { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Entity<Ads>()
            .HasOptional(a => a.Picture)
            .WithMany()
            .HasForeignKey(a => a.PictureId)
            .WillCascadeOnDelete();

        modelBuilder.Entity<Company>()
           .HasOptional(a => a.Logo)
           .WithMany()
           .HasForeignKey(a => a.LogoId)
           .WillCascadeOnDelete();

        modelBuilder.Entity<Company>()
           .HasOptional(a => a.Catalog)
           .WithMany()
           .HasForeignKey(a => a.CatalogId)
           .WillCascadeOnDelete();
    }
}

this code is not working in EF migration!

I want :
– every company has optional Logo or Catalog and every Ads has optional Picture.
– the FileUpload record can be deleted directly
– cascade delete

how to do this?!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:41:07+00:00Added an answer on June 14, 2026 at 10:41 pm

    Do you want that a FileUpload is deleted by cascading delete when a Company or Ads is deleted?

    This would make no sense because FileUpload is the principal in all three relationships and Company and Ads are the dependents. You cannot cascading delete the principal when a dependent is deleted. It would fail in most cases anyway because in a one-to-many relationship the FileUpload could be used by many other companies or ads. If you delete one of them and the FileUpload would be deleted by cascading delete you would violate a foreign key constraint for the other companies and ads.

    In your model you could only have a cascading delete so that Company and Ads are deleted when a FileUpload is deleted (which sounds strange to me though). Even that would not work with your Fluent mapping because you have configured multiple cascading delete paths from FileUpload to Company with the two relationships. SQL Server doesn’t allow such relationships. You would have to remove at least one cascading delete.

    In you model you need to delete the FileUpload records manually when a Company and Ads is deleted after your have checked if the FileUpload isn’t used by another company or ads.

    If you know for sure in your business logic that a given FileUpload cannot be used by more than one company or ads and that the same FileUpload is never used by a company and by an ads, it is getting simpler (example for deleting a company):

    using (var context = new TestContext())
    {
        var company = context.Companies
            .Include(c => c.Logo)
            .Include(c => c.Catalog)
            .Single(c => c.CompanyId == companyIdToDelete);
    
        if (company.Logo != null)
            context.FileUploads.Remove(company.Logo);
    
        if (company.Catalog != null && company.Logo != company.Catalog)
            context.FileUploads.Remove(company.Catalog);
    
        context.Companies.Remove(company);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this model: public class Package { public string CustomerName { get; set;
I have a model like: public class MyModel { public string Name {get; set;}
So I have this model setup public class ListOfThings { public int Id {get;set;}
I have this model : public class Person { public string Name { get;
I have this model: public class StationData { public int StationID{ get; set; }
I have this Model public class SalesModelView { [Key] public int SaleId { get;
I have this model: public class ExchangeRate { [Key] public int ExchangeRateID { get;
I have this model: public class User { //other properties public List<Task> Tasks {get;set;}
I have the following model: public class Person { public string Name { get;
Let's say you have this Model: //model public class Stuff { public string Name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.