dbml metadata

  • Thread starter Thread starter Chuck P
  • Start date Start date
C

Chuck P

Is their a way to get the dbml metadata at runtime.
For example I would like to see the datatype and length for the LastName
field.
 
Hello Chuck,

It depends on where you would like to get information.

1) The simple way is to copy your dbml file into output folder. Then, you
can load that file into XMLDocument instance and retrieve each node by
XMLPath.
For example:

System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
xd.Load("DataClassesName.dbml");
System.Xml.XmlNamespaceManager xnm = new
System.Xml.XmlNamespaceManager(xd.NameTable);
xnm.AddNamespace("dbml",
"http://schemas.microsoft.com/linqtosql/dbml/2007");
string dbtype =
xd.DocumentElement.SelectSingleNode("//dbml:Database/dbml:Table[@Name='dbo.T
able_1']/dbml:Type/dbml:Column[@Name='c1']",
xnm).Attributes["DbType"].Value;

2) Another way is to use reflection to access the ColumnAttribute class in
the System.Data.Linq.Mapping namespace.
For example:
Type linqType = typeof(Table_1);
PropertyInfo[] linqProperties = linqType.GetProperties();
foreach (PropertyInfo property in linqProperties)
{
object[] attributes =
property.GetCustomAttributes(typeof(ColumnAttribute), true);
if (attributes.Length > 0)
{
ColumnAttribute attribute = attributes[0] as
ColumnAttribute;
System.Console.WriteLine(attribute.DbType);

}
}
Brian Mains explained this method in his blog. You may refer to:
http://dotnetslackers.com/community/blogs/bmains/archive/2008/03/16/debuggin
g-linq-problems.aspx
[Debugging LINQ Problems]

Hope this helps, Please feel free to let us know if you have any more
concern. We are glad to assist you.
Have a great day,
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks,
I was looking to get the metadata so I could do stuff like checking that
strings were not to long during data entry.

"Wen Yuan Wang [MSFT]" said:
Hello Chuck,

It depends on where you would like to get information.

1) The simple way is to copy your dbml file into output folder. Then, you
can load that file into XMLDocument instance and retrieve each node by
XMLPath.
For example:

System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
xd.Load("DataClassesName.dbml");
System.Xml.XmlNamespaceManager xnm = new
System.Xml.XmlNamespaceManager(xd.NameTable);
xnm.AddNamespace("dbml",
"http://schemas.microsoft.com/linqtosql/dbml/2007");
string dbtype =
xd.DocumentElement.SelectSingleNode("//dbml:Database/dbml:Table[@Name='dbo.T
able_1']/dbml:Type/dbml:Column[@Name='c1']",
xnm).Attributes["DbType"].Value;

2) Another way is to use reflection to access the ColumnAttribute class in
the System.Data.Linq.Mapping namespace.
For example:
Type linqType = typeof(Table_1);
PropertyInfo[] linqProperties = linqType.GetProperties();
foreach (PropertyInfo property in linqProperties)
{
object[] attributes =
property.GetCustomAttributes(typeof(ColumnAttribute), true);
if (attributes.Length > 0)
{
ColumnAttribute attribute = attributes[0] as
ColumnAttribute;
System.Console.WriteLine(attribute.DbType);

}
}
Brian Mains explained this method in his blog. You may refer to:
http://dotnetslackers.com/community/blogs/bmains/archive/2008/03/16/debuggin
g-linq-problems.aspx
[Debugging LINQ Problems]

Hope this helps, Please feel free to let us know if you have any more
concern. We are glad to assist you.
Have a great day,
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Chuck,
Thanks for your reply.

I believe you can get metadata from dbml file (by method #1). Thereby, you
needn't to check the data entry.
Do you have any more concern or you face any further issue? please feel
free to let us know, again. It's my pleasure to assist you.

Have a great day,
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top