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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:41:17+00:00 2026-06-16T00:41:17+00:00

I’m working with WebMatrix and EntityFramework. In my database I have a user table,

  • 0

I’m working with WebMatrix and EntityFramework.

In my database I have a user table, in our business logic a user can create or update the content of another table, so my user class in my model is like this:

[Table("Users")]
public class User
{
    [Key]
    public int UserId { get; set; }

    [MaxLength(50), MinLength(5), Required]
    public string UserName { get; set; }

    [MaxLength(16), MinLength(8), Required]
    public string Password { get; set; }

    [MaxLength(50), Required]
    public string Name{ get; set; }

    [MaxLength(50), Required]
    public string LasName1{ get; set; }

    [MaxLength(50), Required]
    public string LasName2{ get; set; }

    public string token { get; set; }

    public DateTime CreatedDateTime { get; set; }

    public DateTime UpdatedDateTime { get; set; }

    [InverseProperty("CreatedBy")]
    public virtual ICollection<Device> CreatedDevices {get; set;}

    [InverseProperty("UpdatedBy")]
    public virtual ICollection<Device> UpdatedDevices {get; set;}

    [InverseProperty("CreatedBy")]
    public virtual ICollection<Cluster> CreatedClusters {get; set;}

    [InverseProperty("UpdatedBy")]
    public virtual ICollection<Cluster> UpdatedClusters {get; set;}

    [InverseProperty("CreatedBy")]
    public virtual ICollection<Region> CreatedRegions {get; set;}

    [InverseProperty("UpdatedBy")]
    public virtual ICollection<Region> UpdatedRegions {get; set;}
}

For exampl,e my Region class looks like this:

[Table("Regions")]
public class Region
{
    [Key]
    public int RegionId { get; set; }

    [MaxLength(50), MinLength(5), Required]
    public string Name{ get; set; }

    public virtual ICollection<Cluster> Clusters { get; set; }

    public DateTime CreatedDateTime { get; set; }

    public DateTime UpdatedDateTime { get; set; }

    public Usuario CreatedBy { get; set; } 

    public Usuario UpdatedBy { get; set; }
}

This creates the necessary relationships between the entities in the database. Now comes my main doubt, in my Cluster table there’s a CreatedBy_UserId, so when I’m doing the insert or update of an entry in the table, do I have to pass a User object? I’m saying something like this

Cluster cluster = new Cluster()
{
   RegionId = regionId,
   CreatedDateTime = createdDateTime,
   UpdatedDateTime = updatedDateTime,
   CreatedBy = currentUser,
   UpdatedBy = currentUser
};

Or how do I pass an int representing the foreign key to the table?

  • 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-16T00:41:18+00:00Added an answer on June 16, 2026 at 12:41 am

    If you don’t have a foreign key property in your model, then yes, you need to set the navigation properties to establish a relationship. You only must make sure that currentUser is attached to the context – by loading it from the DB or attaching manually. Otherwise EF would also create new users. An example is this:

    using (var context = new MyContext())
    {
        context.Users.Attach(currentUser);
        Cluster cluster = new Cluster()
        {
            RegionId = regionId,
            CreatedDateTime = createdDateTime,
            UpdatedDateTime = updatedDateTime,
            CreatedBy = currentUser,
            UpdatedBy = currentUser
        };
        context.Clusters.Add(cluster);
        context.SaveChanges();
    }
    

    SaveChanges will create an INSERT statement with the correct foreign key values (equal to currentUser.UserId) set.

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

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a jquery bug and I've been looking for hours now, I can't
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.