Asp.Net MVC数据建模 [英] Asp.Net MVC Data Modeling

查看:53
本文介绍了Asp.Net MVC数据建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对asp.net mvc数据建模有疑问。我必须模拟名为`User`和`Conversation`的类。

I have a problem with asp.net mvc data modeling. I have to model class called `User` and `Conversation`.

User.cs

public class User
    {
        public User()
        {
        }

        [Key]
        [Required]
        [DataType(DataType.EmailAddress)]
        [StringLength(50, ErrorMessage = "User name must be less than 50 characters.")]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [StringLength(50, ErrorMessage = "Name must be less than 50 characters.")]
        [Display(Name = "Name")]
        public string Name { get; set; }

        [Required]
        [DataType(DataType.DateTime)]
        [Display(Name = "Birthdate")]
        public string Birthdate { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime created_at { get; set; }
}

Conversation.cs

Conversation.cs

public class Conversation
    {
        public Conversation()
        {
            status = messageStatus.Sent;
        }

        public enum messageStatus
        {
            Sent,
            Delivered
        }

        [Key, ForeignKey("SenderUser"), Column(Order = 1)]
        [Required]
        [DataType(DataType.EmailAddress)]
        public string SenderUserName { get; set; }
        public virtual User SenderUser { get; set; }

        [Key, ForeignKey("ReceiverUser"), Column(Order = 2)]
        [Required]
        [DataType(DataType.EmailAddress)]
        public string ReceiverUserName { get; set; }
        public virtual User ReceiverUser { get; set; }

        [Key, Column(Order = 3)]
        [DataType(DataType.DateTime)]
        public DateTime created_at { get; set; }

        [Required]
        [DataType(DataType.MultilineText)]
        public string message { get; set; }

        public messageStatus status { get; set; }
    }

我的ChatContext是这样的:

and my ChatContext is like this:

public class ChatContext : DbContext
    {
        public ChatContext() : base("DefaultConnection")
        {
        }

        public static ChatContext Create()
        {
            return new ChatContext();
        }

        public DbSet<User> Users { get; set; }
        public DbSet<Conversation> Conversations { get; set; }
    }

当我运行我的asp应用程序时,我收到此错误:

When I run my asp app, I get this error:

介绍FOREIGN KEY约束表'对话'上的'FK_dbo.Conversations_dbo.Users_SenderUserName'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

无法创建约束或索引。查看以前的错误。

我的设计出了什么问题?

What's wrong with my design?

推荐答案

您可以在EF论坛获得更好的支持,因为您正在处理的是EF Code First方法。

You can get better support at the EF forum, becuase that's what you are dealing with is EF Code First approach.

http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework


这篇关于Asp.Net MVC数据建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆