M
M
Maxim2017-09-27 00:05:54
Database
Maxim, 2017-09-27 00:05:54

Multiple items in an ASP.NET DB cell?

There are 3 tables:

  1. User

    Stores user data
    e3c85f00726ec1342cbe19f8f91f1ff208077025
    CREATE TABLE [dbo].[Users] (
        [Id]        INT           NOT NULL,
        [Login]     NVARCHAR (20) NOT NULL,
        [Password]  NVARCHAR (20) NOT NULL,
        [FirstName] NVARCHAR (15) NOT NULL,
        [SurName]   NVARCHAR (15) NOT NULL,
        [LastName]  NVARCHAR (15) NULL,
        [Status]    NVARCHAR (15) NOT NULL,
        [Balance]   INT           NULL,
        PRIMARY KEY CLUSTERED ([Id] ASC)
    );

    Balance - local money (nullable - because not everyone has it)

  2. Replenishment record
    be431e1c245b88782d63ba56f86a938fe6fb999d
    CREATE TABLE [dbo].[MoneyTrancfer] (
        [Id]     INT            NOT NULL,
        [UserID] INT            NOT NULL,
        [Amount] INT            NOT NULL,
        [Title]  NVARCHAR (150) NOT NULL,
        PRIMARY KEY CLUSTERED ([Id] ASC)
    );


  3. Event

    Goal: For some event you need a certain number of people. (Participation will be rewarded.)
    c7cd71585cc1e583f388b60ed739e261015f15d8
    CREATE TABLE [dbo].[Event]
    (
      [Id] INT NOT NULL PRIMARY KEY, 
        [Name] NVARCHAR(50) NOT NULL, 
      [Reward] INT NOT NULL,
        [Вescription] NVARCHAR(150) NULL, 
        [Amount] TINYINT NOT NULL, 
        [EndTime] DATETIME NOT NULL
        
    )



Questions:
  1. How to make a link in the table to User? (Replace UserID)
  2. How to make several Users in one cell?

PS At the level of the database or Model, but what would be further in the code, it was possible to work comfortably.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Holub, 2017-09-27
@denman1985

1. How to make a link in the table to User? (Replace UserID)

Use JOIN
select Users.LastName, MoneyTrancfer.Amount
from MoneyTrancfer join Users on (MoneyTrancfer.UserID=Users .id)

It is better to make an additional relation table between the two tables.

E
eRKa, 2017-09-27
@kttotto

It's hard to know exactly what you want. You better tell the problem itself.
In which table do you need to make a link to the user? UserID is a link, why replace it?
What does it mean to make several users in one cell? Do you mean one-to-many or many-to-many relationship? It is not clear which table you want to associate with which and which relationship.
You show a DB, and you probably through EF do, show models. Or do you need to show the models in the code how they will look? In general, nothing is clear)
From the comments, I understand that you want to know how the models will look in the code for EF. And once again looking at your tables, I realized that you did not compose them quite correctly. In the User
tablethe Balance field was supposed to point to the Balance table . In this case, it should have been called BalanceId. But again, only for cases where the user can have only one balance. Then in the Balance table it was not necessary to point to the user UserId.
I roughly threw in how it would be in C #

public class User
  {
    public int Id { get; set; }
    public string Login { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual IEnumerable<Event> Events { get; set; } 
    public virtual IEnumerable<Balance> Balances { get; set; }

    public User()
    {
      Events = new List<Event>();
      Balances = new List<Balance>();
    }
  }

  public class Event
  {
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime EndTime { get; set; }

    public virtual IEnumerable<User> Users { get; set; }

    public Event()
    {
      Users = new List<User>();
    }
  }

  public class Balance
  {
    public int Id { get; set; }
    public decimal Amount { get; set; }

    public int UserId { get; set; }
    public virtual User User { get; set; }
  }

Note that users are related to the balance as one-to-many: one user can have many accounts, but an account can only have one user. And events with users are connected as many-to-many, i.e. one user can be attached to many events and many users can participate in one event. For such a relationship, EF will create an additional table.
In general, my advice, do not rush into the code right away, read the database on the database and EF, or at least from here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question