M
M
Maxim2017-10-17 22:14:51
ASP.NET
Maxim, 2017-10-17 22:14:51

How to create an ASP.NET controller for a db?

How to create a controller for a db model?

DB
/* Пользователи */
CREATE TABLE [dbo].[Users] (
    [UserID]    INT           NOT NULL,
    [Login]     NVARCHAR (25) NOT NULL,
    [Password]  NVARCHAR (25) NOT NULL,
    [FirstName] NVARCHAR (25) NOT NULL,
    [SurName]   NVARCHAR (25) NOT NULL,
    [LastName]  NVARCHAR (25) NULL,
    [Status]    NVARCHAR (25) NOT NULL,
    [Balance]   INT           NULL,
    PRIMARY KEY CLUSTERED ([Login] ASC, [UserID] ASC)
);


GO
CREATE UNIQUE NONCLUSTERED INDEX [UserID]
    ON [dbo].[Users]([UserID] ASC);

/* События */
CREATE TABLE [dbo].[Events] (
    [EventID]     INT            IDENTITY (1, 1) NOT NULL,
    [EventName]   NVARCHAR (50)  NOT NULL,
    [StartTime]   DATETIME       NOT NULL,
    [EndTime]     DATETIME       NOT NULL,
    [Description] NVARCHAR (150) NULL,
    [Reward]      INT            DEFAULT ((0)) NOT NULL,
    PRIMARY KEY CLUSTERED ([EventID] ASC)
);

/* Связующий элемент */ 
CREATE TABLE [dbo].[EventMember] (
    [UserID]  INT NOT NULL,
    [EventID] INT NOT NULL,
    PRIMARY KEY CLUSTERED ([UserID] ASC, [EventID] ASC),
    CONSTRAINT .[User]].[UserID]]] FOREIGN KEY ([UserID]) REFERENCES [dbo].[Users] ([UserID]),
    CONSTRAINT .[Events]].[EventID]]] FOREIGN KEY ([EventID]) REFERENCES [dbo].[Events] ([EventID])
);

Model
59e655a99006f635611681.png
Создано автоматически
public partial class User
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public User()
        {
            this.EventMembers = new HashSet<EventMember>();
        }
    
        public int UserID { get; set; }
        public string Login { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string SurName { get; set; }
        public string LastName { get; set; }
        public string Status { get; set; }
        public Nullable<int> Balance { get; set; }
        public int EventMemberUserID { get; set; }
        public int EventMemberEventID { get; set; }
        public int EventMemberUserID1 { get; set; }
        public int EventMemberEventID1 { get; set; }
    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<EventMember> EventMembers { get; set; }
    }

public partial class EventMember
    {
        public int UserID { get; set; }
        public int EventID { get; set; }
    
        public virtual Event Event { get; set; }
        public virtual User User { get; set; }
    }

public partial class Event
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Event()
        {
            this.EventMembers = new HashSet<EventMember>();
        }
    
        public int EventID { get; set; }
        public string EventName { get; set; }
        public System.DateTime StartTime { get; set; }
        public System.DateTime EndTime { get; set; }
        public string Description { get; set; }
        public int Reward { get; set; }
    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<EventMember> EventMembers { get; set; }
    }


When trying to create a controller with read and write actions with pages, an error appears:
59e656899a2bc332213158.png
How to create a controller so that there is no error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2017-10-17
@Got_Oxidus

You need a key field, a unique field that will identify the record, as mentioned above, add the Id property or if you have your own identifier, then specify it with the [Key] attribute

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question