Question

First, a little bit of context

I have one desktop application which uses a Paradox database and I am developing a new ASP.NET application using SQL Server to replace the current desktop app.

The transition will happen in tho phases. The first version of the web app will use SQL Server and Paradox at the same time. Legacy entities data will come from Paradox and data from new entities will come from SQL Server. So the desktop and web versions of the app can be used interchangeably.

In the second phase all data from Paradox will be imported to SQL Server. The old desktop app (and the Paradox db) won't be used anymore.

Now, the technical question

Imagine a simple model with two entities:

public class Customer
{
    public Int32 ID { get; set; }
    public String Name { get; set; }

    virtual public IList<User> MyUsers { get; set; }
}

public class User
{
    public Int32 ID { get; set; }
    public String Name { get; set; }

    virtual public Customer MyCustomer { get; set; }
}

public class SqlServerContext : DbContext
{
    public SqlServerContext()
    {
        base.Configuration.ValidateOnSaveEnabled = false;
        base.Configuration.LazyLoadingEnabled = true;
        base.Configuration.ProxyCreationEnabled = true;
        base.Configuration.AutoDetectChangesEnabled = true;
    }
    public DbSet<User> Users { get; set; }
    public DbSet<Customer> Customers { get; set; }
}

When I use only SQL Server for both entities everything works fine. But now I want to retrieve customer data from Paradox and user data from SQL Server.

I then create a repository for the customers, with an underlying OleDbConnection using the Paradox provider. Of course, Entity Framework still creates the Customers table in SQL Server, since User has a Customer property.

I then removed the database relationship (FK) between User and Customer. Now I am able to insert a User related to a Customer, even though they're in different databases.

The problem is: when I try to retrieve a user from db [eg: context.Users.Find(id)] I can read the user's ID and Name, but user.Customer is null. I expected to get the customer ID through the user.Customer.ID property, since this data is in the Users table in SQL Server.

Am I going to the right path? If so, how can EF bring the customer ID for the user?

And if not, what would be a good approach for this scenario? I don't want to expose foreign key properties in the model (i.e: i don't want to have a int CustomerId property in the User entity).

Thanks in advance!

Was it helpful?

Solution

When you access user.MyCustomer, EF will issue a select statement to the Customers table and then materialize a Customer object. But in your case the table is empty or non existent.

What you can do is include a scalar property CustomerID on User class

public class User
{
    public Int32 ID { get; set; }
    public String Name { get; set; }

    public int? CustomerID { get; set; }//assuming the db column name is also CustomerID
    virtual public Customer MyCustomer { get; set; }
}

Then map the relationship as follows

public class SqlServerContext : DbContext
{
    public SqlServerContext()
    {
        base.Configuration.ValidateOnSaveEnabled = false;
        base.Configuration.LazyLoadingEnabled = true;
        base.Configuration.ProxyCreationEnabled = true;
        base.Configuration.AutoDetectChangesEnabled = true;
    }
    public DbSet<User> Users { get; set; }
    public DbSet<Customer> Customers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       modelBuilder.Entity<User>()
       .HasOptional(u => u.User)
       .WithMany(c => c.MyUsers)
       .HasForeignKey(u => u.CustomerID);

    }
}

Then you can access the customer ID property using user.CustomerID instead of user.Customer.ID.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top