English 中文(简体)
标注后的财产
原标题:Loading property after object has been attached

我曾试图在这里问话之前就去讨论这个问题,我无法找到任何解决办法。

我有两个班级和一个单向地图。

I ́m试图附上一个新物体

    public class MyContext : DbContext
    {
        public IDbSet Operacoes { get; set; }
        public IDbSet Apontamentos { get; set; }
    }

    public class Operacao
    {
        public string Filial { get; set; }
        public string Codigo { get; set; }
        public string Descricao { get; set; }
    }

    public class Apontamento
    {
        public int Id { get; set; }
        public string Filial { get; set; }
        public string OperacaoCodigo { get; set; }
        public virtual Operacao Operacao { get; set; }
    }

    public class OperacaoMap : EntityTypeConfiguration
    {
        public OperacaoMap()
        {
            ToTable("za6010");
            HasKey(x => new { x.Filial, x.Codigo })
                .Property(x => x.Codigo).HasColumnName("za6_cod");

            Property(x => x.Descricao).HasColumnName("za6_desc");
        }
    }



    public class ApontamentoMap : EntityTypeConfiguration
    {
        public ApontamentoMap()
        {
            ToTable("za4010");

            HasKey(x => new { x.Filial, x.Id });

            Property(x => x.OperacaoCodigo)
                .HasColumnName("za4_oper");

            //
            HasRequired(x => x.Operacao)
                .WithMany()
                .HasForeignKey(x => new { x.Filial, x.OperacaoCodigo })
                .WillCascadeOnDelete(false);
        }
    }

    public static class Program
    {
        static void main()
        {
            //this not work, and I need it to work.
            var context = new MyContext();
            var newObj = new Apontamento
            {
                Filial = "01",
                OperacaoCodigo = "001"
            };
            context.Apontamentos.Attach(newObj);
            var desc = newObj.Operacao.Descricao; // Here Operacao Property is null

            //this works
            var newObjTmp = new Apontamento
            {
                Filial = "01",
                OperacaoCodigo = "001"
            };
            var operacao = context.Operacoes.Where(x => x.Codigo == "001");
            context.Apontamentos.Attach(newObj);
            var descTmp = newObjTmp.Operacao.Descricao; // Operacao Property is ok.

        }
    }
最佳回答

您的第一例工作是,因为贵实体没有被动态的代理和导航财产包裹起来,不能装上zy。 引证:

var context = new MyContext();
var newObj = context.Apontamentos.Create();
newObj.Filial = "01",
nowObj.OperacaoCodigo = "001"

context.Apontamentos.Attach(newObj);
var desc = newObj.Operacao.Descricao;

您还可以继续明确使用你目前的解决方案和装载财产:

 var context = new MyContext();
 var newObj = new Apontamento
     {
         Filial = "01",
         OperacaoCodigo = "001"
     };
 context.Apontamentos.Attach(newObj);
 context.Entry(newObj).Reference(o => o.Operacao).Load();
 var desc = newObj.Operacao.Descricao;
问题回答

暂无回答




相关问题
named connection not found (Entity framework problem)

I m building multi-project Application where some UserControl, the user control has a Entitymodel object (myDBContainer db = new myDBContainer()), when i drop my user control on my a form i got the ...

EF4 POCO - Updating a navigation property

I have a Recommendation object and a FeedbackLevel object and the FeedbackLevel object is a navigation property inside a Recommendation object Recommendation int EntityKey; FeedbackLevel Level; ...

How can I add constraints to an ADO.NET Entity?

I know how to mark a group of fields as primary key in ADO.NET entities but i haven t found a way to declare unique constraints or check constraints. Is this feature missing on the designer or on the ...

Using sql date data type and EF4

I have a table w/ a sql date data type. When I look at the EDM markup, the storage element reflects this. The conceptual entity has a data type of DateTime (there doesn t appear to be a Date data type)...

RIA services, EF and stored procs

I have this problem for 2 months now How can I use stored procedures whith RIA. I was using LinqToSql and everithing works fine. I ve made an class in designer and mapped it to a SP. Now in EF I ...

Mocking Entity Context in EF4

I am using VS2010 B2 and EF4 B2 and trying to use Rhino Mocks to mock the entity context generated by EEF. var context = MockRepository.GenerateMock<SomeDBEntities>(); IObjectSet<TxMode> ...

热门标签