简体   繁体   中英

Trying to Configure a One-To-One relationship in ASP.NET MVC?

So, I've been trying to solve this problem for about a week now and have looked everywhere online and just can't seem to find a solution. I'm new to c# and .NET so I don't entirely understand everything that i am doing. Basically i am building a project management app. Each project in the application has a Projected Timeline that shows future dates for when each status should be completed by. I am trying to configure a One-To-Zero-or-One relationship between aa project and it's status.

Each time a project is created the "projected timeline" is created that I am going to use later on.

The problem i am having is that whenever i create a new project the information for its status does not show up in the database. My database shows two separate tables, one for the projects and one for the statuses. But the status row does not get filled in whenever a project is created. Could someone maybe point me in the correct direction and explain what i am doing wrong?

Here is my Project Model

public class Project
    {
        public Project()
        {
            Status Status = new Status();
        }

        [Key]
        public int ProjectID { get; set; }
        public string ProjectTitle { get; set; }

        public virtual Status Status { get; set; }

    }
}

Here is my Status Model

public class Status
    {
        public Status()
        {
            GenerateTimeline();
        }
        [Key,ForeignKey("Project")]
        public int ProjectID { get; set; }

        public virtual Project Project { get; set; }

        public DateTime CreatedDate { get; private set; }
        public DateTime TimeUntilConsultation { get; private set; }
        public DateTime TimeUntilChecklist { get; private set; }
        public DateTime TimeUntilFirstDesign { get; private set; }

        public void GenerateTimeline()
        {
            CreatedDate = DateTime.Now.Date;
            TimeUntilConsultation = CreatedDate.AddBusinessDays(10);
            TimeUntilChecklist = CreatedDate.AddBusinessDays(20);
            TimeUntilFirstDesign = CreatedDate.AddBusinessDays(34);
        }

    }

And here is my Database Context

public class TestDBContextThree : DbContext
    {

        public TestDBContextThree() : base("TestDBContextThree")
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<TestDBContextThree>());
        }
        public DbSet<Project> Projects { get; set; }
        public DbSet<Status> Statuses { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Entity<Project>().HasRequired(u => u.Status);
        }

    }

And here is my controller for creating a new project

// POST: Projects/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ProjectID,ProjectTitle,SelectedStatus")] Project project, Status Status)
        {
            if (ModelState.IsValid)
            {
                db.Statuses.Add(Status);
                db.Projects.Add(project);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(project);
        }

Lol, i just solved my own problem thanks to Shyju's comment.

I was not adding aa valid status entity whenever the project was being created.

Here is my new controller for creating projects. Originally, I had Status Status instead of Status status .

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ProjectID,ProjectTitle,SelectedStatus")] Project project, Status status)
        {
            if (ModelState.IsValid)
            {
                db.Statuses.Add(status);
                db.Projects.Add(project);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(project);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM