locked
Fluent API EF Code First RRS feed

  • Question

  • Hello!
    I have this code:
    static void Main(string[] args)
            {
                TestContext context = new TestContext("Test");
                var currency = new Currency
                {
                    Value = "USD"
                };
                context.SaveChanges();
                var customer = new Customer
                {
                    Name = "Artyom",
                    CurrencyId = currency.Id
                };
                context.SaveChanges();
    
                Console.WriteLine(customer.Currency.Value);
                Console.ReadLine();
            }
        }
    
        public class TestContext : DbContext
        {
            public DbSet<Customer> Customers { get; set; }
            public DbSet<Currency> Currencies { get; set; }
    
            public TestContext(string connectionString)
                : base(connectionString)
            {
    
            }
    
            #region Overrides of DbContext
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Configurations.Add(new CustomerMap());
                modelBuilder.Configurations.Add(new CurrencyMap());
    
                base.OnModelCreating(modelBuilder);
            }
    
            #endregion
        }
    
        public class Customer
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public Currency Currency { get; set; }
            public int CurrencyId { get; set; }
        }
    
        public class Currency
        {
            public int Id { get; set; }
            public string Value { get; set; }
        }
    
        public class CustomerMap : EntityTypeConfiguration<Customer>
        {
            public CustomerMap()
            {
                HasRequired(e => e.Currency).WithMany().HasForeignKey(e => e.CurrencyId);
            }
        }
    
        public class CurrencyMap : EntityTypeConfiguration<Currency>
        {
            public CurrencyMap()
            {
                Property(e => e.Value).IsRequired();
            }
        }

    When I enter CurrencyId in customer, I want EF to load Currency for it, because I set a relationship between them, but it doesn't do it and Currency property is null :(
    Can you explain why and how can I realize what I want?
    Friday, December 20, 2013 2:25 PM

Answers

  • Hello,

    >> When I enter CurrencyId in customer, I want EF to load Currency for it, because I set a relationship between them, but it doesn't do it and Currency property is null :(

    Because the data is not inserted into the database actually.

    Adding this “context.Currencies.Add(currency);” before the first “context.SaveChange()” and add this “context.Customers.Add(customer);” before the second “context.SaveChange()”, then it will print:

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Friday, December 27, 2013 9:52 AM
    Monday, December 23, 2013 2:05 AM