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

  • Home
  • SEARCH
  • 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 8229627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:51:34+00:00 2026-06-07T16:51:34+00:00

Do DataAnnotations do the same thing as Fluent API methods? For example, does .HasRequired

  • 0

Do DataAnnotations do the same thing as Fluent API methods?

For example, does .HasRequired == [Required]?

.HasKey == [Key]?

Must I mark class key with [Key] or use .HasKey if I do not want my table pk field generated as ClassNameClassNameID if I am using ClassNameID format for my pk property?

Can I use a combonation of DataAnnotations and Fluent API; or, is it one or the other?

Must I map M:M relationships for both classes involved i.e.

public class Foo
{
    public ICollection<Bar> Bars
}

public class Bar
{
    public ICollection<Bar> Foos
}

public class Context : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Foo>()
            .HasMany(f => f.Bars).WithMany(b => b.Foos)
            .Map(t =>
                {
                    t.MapLeftKey("FooID");
                    t.MapRightKey("BarId");
                    t.ToTable("FooBar");
                });

         modelBuilder.Entity<Bar>()
            .HasMany(b => b.Foos).WithMany(f => f.Bars)
            .Map(t =>
                {
                    t.MapLeftKey("FooID");
                    t.MapRightKey("BarId");
                    t.ToTable("FooBar");
                });
}

Thanks.

  • 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-07T16:51:35+00:00Added an answer on June 7, 2026 at 4:51 pm

    Can I use a combonation of DataAnnotations and Fluent API; or, is it
    one or the other?

    Yes, you can use a combination. However it’s generally better to pick one or the other. I prefer fluent, as it keeps certain persistence concerns out of the entity classes.

    Do DataAnnotations do the same thing as Fluent API methods?

    For example, does .HasRequired == [Required]?

    .HasKey == [Key]?

    Yes. Also, .IsRequired == [Required]

    Must I map M:M relationships for both classes involved

    No, you only need to map one side of the relationship. You can map both sides, but if you change the relationship, you will then need to modify both sets of code. Also, when you map both sides, both mappings must resolve to the same result (i.e. same foreign key declaration in the db).

    Must I mark class key with [Key] or use .HasKey if I do not want my
    table pk field generated as ClassNameClassNameID if I am using
    ClassNameID format for my pk property?

    Yes, unless your property name is Id or PersonId / ProductId / OrderId / WhateverEntityNameId. By convention, EF will guess that these are your key properties. However it never hurts to make this explicit by invoking .HasKey on your modelBuilder.

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

Sidebar

Related Questions

DataAnnotations does not work with buddy class. The following code always validate true. Why
Model: using System.ComponentModel.DataAnnotations; using MySite.Validators; namespace MySite.Models { public class AddItem { [Required(ErrorMessage =
I'm using DataAnnotations attributes to validate my model objects. My model class looks similar
How can I access metadata (dataannotations attributes) in my asp.net mvc model class from
I have a business model called Customer which has many required properties (via DataAnnotations)
Is it possible to add a CSS class from DataAnnotations (metadata) when I use
I understand annotating class properties with the basic required and minimum length and getting
I recently used ASP.Net MVC with DataAnnotations and was thinking of using the same
This is the datamodel I have: public class Team { [Key] public int Id
I'm using dataannotations in an MVC2 app and am a little discouraged when trying

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.