使用GridView时的问题

1 view
Skip to first unread message

寻路者

unread,
Dec 14, 2006, 10:45:55 PM12/14/06
to NBear Google Group
例如我有2个实体
public interface T_City:Entity
{

[PrimaryKey]
string City_ID
{
get;
set;
}

[SqlType("nvarchar(16)")]
[NotNull]
string City_Name
{
get;
set;
}

[SqlType("nvarchar(16)")]
[NotNull]
string Province_Name
{
get;
set;
}

[SqlType("nvarchar(256)")]
string Explain
{
get;
set;
}
[FkQuery("City_ID", Contained = true, LazyLoad = true)]
[SerializationIgnore]
T_Consumer[] Consumers
{
get;
set;
}
}

public interface T_Consumer:Entity
{

[PrimaryKey]
string Consumer_ID
{
get;
set;
}

[NotNull]
string PWD
{
get;
set;
}

[SqlType("nvarchar(2)")]
string Sex
{
get;
set;
}

DateTime Birthday
{
get;
set;
}

[SqlType("nvarchar(16)")]
string Education
{
get;
set;
}

string City_ID
{
get;
set;
}
}
想用GridView显示Consumer的内容,但是我想显示的是City_Name,而City_ID是个无意义的主键值,是不是必须要建个视图实体才行?有没有好的方法,因为如果建视图的话,在表多的话会很多.

寻路者

unread,
Dec 14, 2006, 10:55:20 PM12/14/06
to NBear Google Group
例如我现在2个实体
public interface T_Consumer:Entity
{

DateTime Birthday
{
get;
set;
}

string City_ID
{
get;
set;
}

}

public interface T_City:Entity
{

我现在用GridView来显示Consumer的内容,不过City_ID是无意义的外键,想显示对应City_Name,是不是一定要建视图类?这样的话,如果表比较多的话,视图类会很多,而且不好控制,有没有什么方法,使得City_ID
与City_Name关联起来?

寻路者

unread,
Dec 15, 2006, 12:07:59 AM12/15/06
to NBear Google Group
例如我有2个实体
public interface T_City:Entity
{

[PrimaryKey]
string City_ID
{
get;
set;
}

[SqlType("nvarchar(16)")]
[NotNull]
string City_Name
{
get;
set;
}

[SqlType("nvarchar(16)")]
[NotNull]
string Province_Name
{
get;
set;
}

[SqlType("nvarchar(256)")]
string Explain
{
get;
set;
}

[FkQuery("City_ID", Contained = true, LazyLoad = true)]
[SerializationIgnore]

V_Consumer[] Vconsumers
{
get;
}
}
public interface T_Consumer:Entity
{

[PrimaryKey]
string Consumer_ID
{
get;
set;
}

[NotNull]
string PWD
{
get;
set;
}

[SqlType("nvarchar(2)")]
string Sex
{
get;
set;
}


string City_ID
{
get;
set;
}
}
我现在用GridView来显示Consumer,其中
City_ID只是外键,它的值没什么意义,我想显示CIty_Name,这样是不是一定要建个视图实体?如果是的话,在表多的情况下,会需要很多视图,很不方便的,有什么好解决方法么?

寻路者

unread,
Dec 15, 2006, 1:19:49 AM12/15/06
to NBear Google Group
不好意思,一下子连发了

Teddy

unread,
Dec 15, 2006, 1:29:20 AM12/15/06
to nbear...@googlegroups.com
可以将City_ID改为一个City实体。然后为Consumer写一个partial class,添加一个CityName属性,简单返回Consumer的City实体的Name属性。

On 12/15/06, 寻路者 <1814...@qq.com> wrote:
不好意思,一下子连发了

寻路者

unread,
Dec 15, 2006, 1:32:15 AM12/15/06
to NBear Google Group
能简单写下代码么?看的不是很明白

Teddy

unread,
Dec 15, 2006, 1:42:00 AM12/15/06
to nbear...@googlegroups.com
把City_ID属性改为:
 
       [FkReverseQuery]
       CityCity
       {
           get;
           set;
       }

 
然后在你的Entities所在的project建一个独立的Comsumer.cs文件,包含下面的内容:
 
namespace YourNamespace
{
  public partial class Comsumer : NBear.Common.Entity
  {
    public string CityName { get { if (City != null) return City.City_Name else return null; } }
  }
}
 
On 12/15/06, 寻路者 <1814...@qq.com> wrote:
能简单写下代码么?看的不是很明白

寻路者

unread,
Dec 19, 2006, 10:40:13 PM12/19/06
to NBear Google Group
用了这个我怎么添加Consumer的City_ID?

寻路者

unread,
Dec 19, 2006, 10:52:42 PM12/19/06
to NBear Google Group
我在添加City_ID时发现Consumer里没有CIty_ID只有City,而在对Consumer.City.City_ID付值时出现:未将对象引用设置到对象的实例
这样的错误提示.

Teddy

unread,
Dec 19, 2006, 10:55:25 PM12/19/06
to nbear...@googlegroups.com
你可以new一个City,设置city.City_ID=id再comsumer.City = city;

On 12/20/06, 寻路者 <1814...@qq.com> wrote:
我在添加City_ID时发现Consumer里没有CIty_ID只有City,而在对Consumer.City.City_ID付值时出现:未将对象引用设置到对象的实例
这样的错误提示.


寻路者

unread,
Dec 19, 2006, 11:07:01 PM12/19/06
to NBear Google Group
这样会不会增加数据库的压力?

Teddy

unread,
Dec 19, 2006, 11:10:18 PM12/19/06
to nbear...@googlegroups.com
使用City代替CityID时,取Comsumer.City数据自然要多查一次数据库的。所以要根据实际情况来取舍。

On 12/20/06, 寻路者 <1814...@qq.com> wrote:
这样会不会增加数据库的压力?

寻路者

unread,
Dec 19, 2006, 11:33:42 PM12/19/06
to NBear Google Group
而且这样改了,我发现删除和添加会很麻烦.实在不好取舍.

Teddy

unread,
Dec 19, 2006, 11:40:09 PM12/19/06
to nbear...@googlegroups.com
还有一个办法是在保留City_ID的情况下,为 Comsumer增加一个 CustomQuery的City属性,这样能既保留City_ID操作的方便性,又能获得查询City的便利:
 
//CustomQuery的第一个参数的意思是:City实体的City_ID属性(由{City_ID}表示)的值等于当前Comsumer实例的City_ID属性(由@City_ID表示)的值。
[CustomQuery("{City_ID} = @City_ID")]
City { get; set; }
 
Teddy
 
On 12/20/06, 寻路者 <1814...@qq.com> wrote:
而且这样改了,我发现删除和添加会很麻烦.实在不好取舍.

寻路者

unread,
Dec 19, 2006, 11:51:32 PM12/19/06
to NBear Google Group
好的,我去试一下
Reply all
Reply to author
Forward
0 new messages