0

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!

4

1 回答 1

1

当您访问时user.MyCustomer,EF 将向Customers表发出一个 select 语句,然后具体化一个Customer对象。但在你的情况下,表是空的或不存在。

您可以做的是CustomerIDUser类中包含一个标量属性

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; }
}

然后映射关系如下

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);

    }
}

user.CustomerID然后,您可以使用而不是访问客户 ID 属性user.Customer.ID

于 2011-09-06T00:19:54.603 回答