开发者

Code First Object not properly instantiating

开发者 https://www.devze.com 2023-04-13 09:35 出处:网络
I havea class department inheriting from activeentity public class ActiveEntity : Entity, IActive { public ActiveEntity()

I have a class department inheriting from activeentity

public class ActiveEntity : Entity, IActive
    {
        public ActiveEntity()
        {
            IsActive = true;
        }
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public Guid Id { get; set; }
        public bool IsActive { get; set; }
        [Timestamp, ScaffoldColumn(false), DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Computed)]
        public Byte[] Timestamp { get; set; }
        [ScaffoldColumn(false)]
        public string CreationUserId { get; set; }
        [ScaffoldColumn(false)]
        public string LastModifiedUserId { get; set; }
    }
    public class Department:ActiveEntity    
    {
        public Department()
        {
            this.Address = new DepartmentAddress();
        }
        [StringLength(9),开发者_如何学PythonMinLength(9),MaxLength(9)]
        public string Name { get; set; }

        public Guid ManagerId { get; set; }
        [UIHint("AjaxDropdown")]
        public User Manager { get; set; }
        public Guid? AddressId { get; set; }
        public DepartmentAddress Address { get; set; }

        public ICollection<OverheadRate> OverheadRates { get; set; }


    }

I am just using annotations no Fluent API. The data saves to the data Sql Server 2008 just fine however the address object never gets instantiated, even though I have the context use the include

return c.Set<Department>().Include(d => d.Address).Include(d => d.Manager).Where(predicate);

The data is returned I run sql profiler and then run the query it returns the correct data.

Any thoughts or suggestions?


Remove instantiating the address (this.Address = new DepartmentAddress();) in the Department constructor. Instantiating navigation references in the default constructor is evil and has nasty side effects like these:

What would cause the Entity Framework to save an unloaded (but lazy loadable) reference over existing data?

EF 4.1 Code First: Why is EF not setting this navigation property?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号