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 6884181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:28:31+00:00 2026-05-27T05:28:31+00:00

(Since i have a database predefined i can’t let EF recreate it). This the

  • 0

(Since i have a database predefined i can’t let EF recreate it).

This the mapping i use now (works but i want to rewrite using fluent api):

public class League
{        
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid LeagueId { get; set; }
    ....
    #region References

    public virtual ICollection<News> News { get; set; } 

    #endregion
}

public class News
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid NewsId { get; set; }
    public Guid LeagueId { get; set; }
    ....
    #region References

    [ForeignKey("LeagueId")]
    public virtual League League { get; set; }

    #endregion
}

Now, how can i map this using the fluent API?

Update
Wrote this and it works, but is there a simpler version?

modelBuilder.Entity<League>().HasMany(x => x.News).WithRequired(y => y.League).HasForeignKey(c => c.LeagueId);

Update 2
I added those missing from the classes. But the problem is that if i leave it at that and try it, it fails. I need to specify a key somewhere.I can’t let EF create the database and it refuses to just operate with tables.

  • 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-05-27T05:28:32+00:00Added an answer on May 27, 2026 at 5:28 am

    You should look at this article for example: http://www.codeproject.com/Articles/184133/Using-Entity-Framework-4-1-Code-First-with-an-exis
    The base part, is that you should remove default db initializer:

    Database.SetInitializer<YourContext>(null);
    

    And this should prevent EF from trying to change your db or throw errors. It will just try to work with what you give it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database design that's normalized well enough, but now I need to
I have a database of 3D models. I want user can rotate the model
Since I have started using this site, I keep hearing about the Boost library.
Okay, it seems to be a stupid question, since we have this However, up
I have a database that I built in SQLite browser, and it works fine.
I haven't been able to find an answer to this. I have a database
i have database table with few text fields which i use to filter data
I have a pre-defined database that I want to map using Entity Framework 4
I have a sql server 2008 database, the code (now corrected) has accidently overwrite
I have researched this to no avail and thought I'd inquire here since the

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.