ASP.NET MVC实体框架使用codefirst方法重新创建表

时间:2018-01-21 10:42:17

标签: asp.net-mvc entity-framework

我在ASP.NET MVC工作。我使用Entity Framework代码优先方法创建了一个表,但不幸的是我删除了表。我怎样才能重新创建我的桌子?

当我只想用类重新创建表时,它显示错误

  

dbo_Event不存在且您没有权限

请帮帮我。

public class Event
{
    [Key]
    public int Id { get; set; }
    public string EventName { get; set; }
    public string Category { get; set; }
    //public string Speaker { get; set; }
    // public string Topic { get; set; }
    public DateTime Date { get; set; }
    public string Type { get; set; } // This shows which type it belong to i.e
    public string Venue { get; set; }
    public string Description { get; set; }
    public string SuperVision { get; set; }
    public Nullable<int> Department { get; set; }
    public Nullable<int> Designation { get; set; }
    public byte[] Image { get; set; }
    //[DataType(DataType.Upload)]
    //public HttpPostedFileBase file { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }

    //these are the link tables
    public int EvntID { get; set; }
    public int DepartID { get; set; }

    public int DesigID { get; set; }

    public string Speaker { get; set; }
    public string Topic { get; set; }
}

以下是行动方法:

[HttpPost]
public ActionResult Create(Event events, HttpPostedFileBase files)
{
        Event evnt = new Event();
        string ext = Path.GetExtension(files.FileName).ToUpper();

        if (ext == ".JPG" || ext == ".JPEG" || ext == ".PNG")
        {
            evnt.EventName = events.EventName;
            evnt.Category = events.Category;
            evnt.Date = events.Date;
            evnt.Venue = events.Venue;
            evnt.Description = events.Description;
            evnt.SuperVision = events.SuperVision;

            MemoryStream stream = new MemoryStream();
            //files.InputStream.CopyTo(stream);

            byte[] image = stream.ToArray();

            evnt.Image = image;

            evnt.StartDate = events.StartDate;
            evnt.EndDate = events.EndDate;

            db.Event.Add(evnt);
            db.SaveChanges();

            return View("Index");
        }
        else
        {
            return HttpNotFound();
        }
}

1 个答案:

答案 0 :(得分:0)

转到Nuget包管理器 - &gt;包管理控制台  启用 - 迁移  2. add-migration Initial  3. update-database