0

私はこのモデルを持っています

namespace ProjectTimer.Models
{
    public class TimerContext : DbContext
    {
        public TimerContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<Project> Projects { get; set; }
        public DbSet<ProjectTimeSpan> TimeSpans { get; set; }
    }

    public class DomainBase
    {
        [Key]
        public int Id { get; set; }
    }

    public class Project : DomainBase
    {
        public UserProfile User { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public IList<ProjectTimeSpan> TimeSpans { get; set; }
    }

    [ComplexType]
    public class ProjectTimeSpan
    {
        public DateTime TimeStart { get; set; }
        public DateTime TimeEnd { get; set; }
        public bool Active { get; set; }
    }
}

このアクションを使おうとすると、例外が発生しますThe type 'ProjectTimer.Models.ProjectTimeSpan' has already been configured as an entity type. It cannot be reconfigured as a complex type.

public ActionResult Index()
        {
            using (var db = new TimerContext())
            {
                return View(db.Projects.ToList);
            }
        }

ビューはモデルを使用しています@model IList<ProjectTimer.Models.Project>

なぜこれが起こるのかについて、誰かが光を当てることができますか?

4

1 に答える 1

0

あなたのIList<ProjectTimeSpan>プロパティは EF でサポートされていません。複合型は常に別のエンティティ型の一部である必要があり、複合型を単独で使用することはできません。絶対ProjectTimeSpanに複合型にする必要がある場合は、キーと のみを含むダミーのエンティティ型を作成し、の型をその新しい型のリストにProjectTimeSpan変更する必要があります。Project.TimeSpans

于 2012-09-16T22:02:08.287 に答える