由于实体类型'IdentityUserRole <int>'的'属性'xx'具有临时值,因此无法在数据库中播种表

时间:2019-06-11 16:45:51

标签: asp.net .net entity-framework .net-core entity-framework-core

我正在尝试显示实体的可用日期和可用时间-一位讲师,但是,当我尝试为新实体播种一些数据并且错误消息不均匀时(或在至少看起来像)与我要添加的实体有关。

这个想法是讲师会列出可用日期。

开始调试后,我在输出中遇到的错误是:

实体类型“ IdentityUserRole ”上的属性“ UserId”具有一个临时值。要么显式设置一个永久值,要么确保将数据库配置为为此属性生成值。

应用程序随后将继续正常运行,但是我尝试播种的数据永远不会存储在数据库中。

我确定我正在忽略某些东西或配置了错误的东西,我们将不胜感激,如果需要更多信息,请告诉我。

如果有帮助,我正在将.NET Core 2.2与EF Core 2.2和SQL Server作为数据库提供一起使用

我已经尝试删除并重新创建数据库,我再次检查了这些关系,但是似乎没有任何帮助。我还要确保播种顺序正确。

以下是导致错误的实体:

public class AvailableDate
{
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int AvailableDateId { get; set; }

        public DateTime Date { get; set; }

        public List<AvailableDateAvailableTime> AvailableDateAvailableTimes { get; set; 
}

public class AvailableTime
{
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int AvailableTimeId { get; set; }

        public TimeSpan From { get; set; }
        public TimeSpan To { get; set; }

        public List<AvailableDateAvailableTime> AvailableDateAvailableTimes { get; set; }

}


public class AvailableDateAvailableTime
{
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int AvailableDateAvailableTimeId { get; set; }

        public int AvailableDateId { get; set; }
        public AvailableDate AvailableDate { get; set; }
        public int AvailableTimeId { get; set; }
        public AvailableTime AvailableTime { get; set; }
}


Seed method - if I put a breakpoint at the end I can see that the correct values are filled in, they just never get saved to the database.

 private static IEnumerable<AvailableDate> FillAvailableDates()
 {
            var today = DateTime.Now;

            var availableDates = new List<AvailableDate>();

            for (int i = 0; i < 14; i++)
            {
                availableDates.Add(new AvailableDate
                {
                    Date = today.Date
                });

                today = today.AddDays(1);
            }

            return availableDates;
}

// Seeding available dates - never succeeds even though the data is correct
private static IEnumerable<AvailableDate> FillAvailableDates()
{
            var today = DateTime.Now;

            var availableDates = new List<AvailableDate>();

            for (int i = 0; i < 14; i++)
            {
                availableDates.Add(new AvailableDate
                {
                    Date = today.Date
                });

                today = today.AddDays(1);
            }

            return availableDates;
}

一旦我注释掉该表的种子方法调用,该错误就不再存在:

        //if (!context.AvailableDates.Any())
        //{
        //    context.AddRange(FillAvailableDates());
        //    await context.SaveChangesAsync();
        //}

播种可用时间很好

private static IEnumerable<AvailableTime> FillAvailableTimes()
{
            var timespan = new TimeSpan(0, 30, 0);

            var timeblocks = new List<AvailableTime>();

            var today = DateTime.Now;

            var initialTime = new DateTime(today.Year, today.Month, today.Day, 08, 00, 0);


            for(int i = 0; i < 24; i++)
            {
                timeblocks.Add(new AvailableTime
                {
                    From = initialTime.TimeOfDay,
                    To = initialTime.TimeOfDay + timespan
                });

                initialTime += timespan;
            }

            return timeblocks;
}

我也有相似的实体,它们具有相同的关系,不会引起问题:


// User base is just a class with some common user info
public class Instructor : UserBase
{
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int InstructorId { get; set; }

        //removed some props for readability...
        public List<AvailableDate> AvailableDates { get; set; }
        public List<InstructorSubject> InstructorSubjects { get; set; }
}

public class Subject
{
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int SubjectId { get; set; }

        public string Name { get; set; }

        public List<InstructorSubject> InstructorSubjects { get; set; }
}

public class InstructorSubject
{
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int InstructorSubjectId { get; set; }

        public int InstructorId { get; set; }
        public Instructor Instructor { get; set; }
        public int SubjectId { get; set; }
        public Subject Subject { get; set; }
}

// Seeding instructors, then subjects, then instructor subjects

private static IEnumerable<InstructorSubject> FillInstructorSubjects()
{
            //  Assign to instructor subjects list
            _instructorSubjects = new List<InstructorSubject>()
            {
                new InstructorSubject()
                {
                    InstructorId = 1,
                    SubjectId = 1
                },

                new InstructorSubject()
                {
                    InstructorId = 1,
                    SubjectId = 2
                }
                //etc...
            }
            return _instructorSubjects;
}


¸¸¸¸

1 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <!--jQuery-->
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

  <!--css-->

  <style type="text/css">
    *{
      margin:0;
      padding:0;
    }
    .carousel{
      overflow:hidden;
      white-space:nowrap;

    }
    .img{
      display:inline;
      position:relative;
      max-width:900px;
      height:auto;
    }
  </style>


  <!--[if lt IE 9]>
         <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

  <title>Title of the document</title>
  </head>

<body>

  <section >
        <div class="carousel">
      <div class="img"><img src="images/slide-show-1.jpg" /></div>
      <div class="img"><img src="images/slide-show-2.jpg" /></div>
      <div class="img"><img src="images/slide-show-3.jpg" /></div>
      <div class="img"><img src="images/slide-show-4.jpg" /></div>
    </div>
    </section>

  <script>

    function slide(){

      var img = $('.img');
      var firstImg = img.first();
      var lastImg = img.last()
      var firstImagePos = firstImg.position().left;

      img.animate({left:firstImagePos-900});

      //alert('First image is '+$('.img img').first().attr('src') + '\
' + 'And First Image Pos is '+firstImagePos);

      if(firstImagePos < -900){
        firstImg.insertAfter(lastImg);
      }

    }

    setInterval(function() {
      //slide();
    }, 2000);


  </script>

</body>

</html>