Simple.Data>未填充的另一个对象内的对象

时间:2015-04-30 04:38:47

标签: simple.data

我有两张表格如下:

表:订单

CREATE TABLE [dbo].[orders](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [ticker] [char](5) NOT NULL,
    [broker_id] [char](12) NOT NULL,
    [type] [smallint] NOT NULL,
    [quantity] [int] NOT NULL,
    [price] [money] NOT NULL,
 CONSTRAINT [PK_orders] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[orders]  WITH CHECK ADD  CONSTRAINT [FK_orders_quotes] FOREIGN KEY([ticker])
REFERENCES [dbo].[quotes] ([ticker])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[orders] CHECK CONSTRAINT [FK_orders_quotes]
GO

ALTER TABLE [dbo].[orders]  WITH CHECK ADD  CONSTRAINT [FK_orders_types] FOREIGN KEY([type])
REFERENCES [dbo].[order_types] ([id])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[orders] CHECK CONSTRAINT [FK_orders_types]
GO

ALTER TABLE [dbo].[orders]  WITH CHECK ADD  CONSTRAINT [FK_orders_users] FOREIGN KEY([broker_id])
REFERENCES [dbo].[users] ([id])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[orders] CHECK CONSTRAINT [FK_orders_users]
GO

表:用户

CREATE TABLE [dbo].[users](
    [id] [char](12) NOT NULL,
    [password] [varchar](60) NULL,
    [first_name] [varchar](20) NULL,
    [last_name] [varchar](20) NULL,
    [email] [varchar](20) NULL,
    [date_of_birth] [datetime] NULL,
    [capital] [money] NULL,
    [created_by] [varchar](12) NULL,
    [created_date] [datetime] NULL,
 CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[users] ADD  DEFAULT ((0.0)) FOR [capital]
GO

我有两个类映射到上面的表:

顺序

public class Order
{
    public Order(string ticker, User user, OrderTypes type, int quantity, decimal price)
    {
        this.Ticker = ticker;
        this.Broker = user;
        this.Type = type;
        this.Quantity = quantity;
        this.Price = price;
    }
    public Order() { }

    public string Ticker { get; private set; }
    public OrderTypes Type { get; private set; }
    public int Quantity { get; private set; }
    public decimal Price { get; private set; }

    public User Broker { get; private set; }
}

用户

public class User
    {
        public User() { }

        public User(string id, string firstName, string lastName, DateTime dateOfBirth, string email, decimal capital)
        {
            this.Id = id;
            this.FirstName = firstName;
            this.LastName = lastName;
            this.DateOfBirth = dateOfBirth;
            this.Email = email;
            this.Capital = capital;
        }

        public User(string id, string firstName, string lastName, DateTime dateOfBirth, string email) : this(id, firstName, lastName, dateOfBirth, email, 0) { }

        public string Id { get; private set; }
        public string FirstName { get; private set; }
        public string LastName { get; private set; }
        public DateTime DateOfBirth { get; private set; }
        public string Email { get; private set; }
        public decimal Capital { get; private set; }
    }

最后,我有两行代码:

db.Orders.Insert(id: 1, broker_id: "john.doe", quantity: 100);
Order order = db.Orders.Get(1);

对象订单具有属性" Id"和"数量"填充,但对象"经纪人"是空的。

所以我尝试检索如下所示的数据,但仍然没有运气:Broker对象始终为NULL:

var order = db.Orders.FindAllById(1)
                .Join(db.Users.As("Broker"), out user)
                .On(Id: db.Orders.Broker_Id)
                .With(user.As("Broker"))
                .FirstOrDefault();

WITH __Data AS (SELECT [dbo].[orders].[id], ROW_NUMBER() OVER(ORDER BY [dbo].[orders].[id]) AS [_#_]
from [dbo].[orders] JOIN [dbo].[users] [Broker] ON ([Broker].[id] = [dbo].[orders].[broker_id]) WHERE [dbo].[orders].[id] = @p1)
SELECT [dbo].[orders].[id],[dbo].[orders].[ticker],[dbo].[orders].[broker_id],[dbo].[orders].[type],[dbo].[orders].[quantity],[dbo].[orders].[price],[Broker].[id] AS [__withn__Broker__id],[Broker].[password] AS [__withn__Broker__password],[Broker].[first_name] AS [__withn__Broker__first_name],[Broker].[last_name] AS [__withn__Broker__last_name],[Broker].[email] AS [__withn__Broker__email],[Broker].[date_of_birth] AS [__withn__Broker__date_of_birth],[Broker].[capital] AS [__withn__Broker__capital],[Broker].[created_by] AS [__withn__Broker__created_by],[Broker].[created_date] AS [__withn__Broker__created_date] FROM __Data JOIN [dbo].[orders] ON [dbo].[orders].[id] = __Data.[id] JOIN [dbo].[users] [Broker] ON ([Broker].[id] = [dbo].[orders].[broker_id]) WHERE [dbo].[orders].[id] = @p1 AND [_#_] BETWEEN 1 AND 1
Parameters:
  @p1 (Int32) = 1
order = db.Orders.FindAllById(1)
            .Join(db.Users.As("Broker"), out user)
            .On(db.Orders.broker_id == user.Id)
            .With(user)
            .FirstOrDefault();

WITH __Data AS (SELECT [dbo].[orders].[id], ROW_NUMBER() OVER(ORDER BY [dbo].[orders].[id]) AS [_#_]
from [dbo].[orders] JOIN [dbo].[users] [Broker] ON ([dbo].[orders].[broker_id] = [Broker].[id]) WHERE [dbo].[orders].[id] = @p1)
SELECT [dbo].[orders].[id],[dbo].[orders].[ticker],[dbo].[orders].[broker_id],[dbo].[orders].[type],[dbo].[orders].[quantity],[dbo].[orders].[price],[Broker].[id] AS [__withn__Broker__id],[Broker].[password] AS [__withn__Broker__password],[Broker].[first_name] AS [__withn__Broker__first_name],[Broker].[last_name] AS [__withn__Broker__last_name],[Broker].[email] AS [__withn__Broker__email],[Broker].[date_of_birth] AS [__withn__Broker__date_of_birth],[Broker].[capital] AS [__withn__Broker__capital],[Broker].[created_by] AS [__withn__Broker__created_by],[Broker].[created_date] AS [__withn__Broker__created_date] FROM __Data JOIN [dbo].[orders] ON [dbo].[orders].[id] = __Data.[id] JOIN [dbo].[users] [Broker] ON ([dbo].[orders].[broker_id] = [Broker].[id]) WHERE [dbo].[orders].[id] = @p1 AND [_#_] BETWEEN 1 AND 1
Parameters:
  @p1 (Int32) = 1
order = db.Orders.With(db.Users.As("Broker")).Get(1); 

WITH __Data AS (SELECT [dbo].[orders].[id], ROW_NUMBER() OVER(ORDER BY [dbo].[orders].[id]) AS [_#_]
from [dbo].[orders] WHERE [dbo].[orders].[id] = @p1)
SELECT [dbo].[orders].[id],[dbo].[orders].[ticker],[dbo].[orders].[broker_id],[dbo].[orders].[type],[dbo].[orders].[quantity],[dbo].[orders].[price] FROM __Data JOIN [dbo].[orders] ON [dbo].[orders].[id] = __Data.[id] WHERE [dbo].[orders].[id] = @p1 AND [_#_] BETWEEN 1 AND 1
Parameters:
  @p1 (Int32) = 1
  
    

注意:将订单分配给变量" var"时,订单会填充用户信息。     所以,如果我这样做:

  

var dynamicOrder = db.Orders.WithUser()。Get(1);

" dynamicOrder"是" Simple.Data.SimpleRecord"其中包含以下属性" System.Collections.Generic.IDictionary.Values"包含"用户"里面的数据。

所以问题似乎是将用户数据转换为用户类。

思想?

0 个答案:

没有答案