Using DbEntry in a SQL Server Project

Feb 17, 2008 at 11:32 PM
I am trying to use DbEntry in a SQL Server Project (VS 2005 C# dabase project), which is a dll assembly. Therefore, I have to use, as recommended in the doc, an embeded resources to include my configuration information. Here is a app.config file in the project and I set this file as Embed Resource for build action:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Lephone.Settings"
type="Lephone.Util.Setting.NameValueSectionHandler, Lephone.Util" />
</configSections>
<Lephone.Settings>
<add key="DataBase"
value="@SqlServer2005 : Context Connection=true" />
</Lephone.Settings>
</configuration>

The connection string is "Context Connection=true" because the dll will be running in SQL server. I use this dll to create some stored procedures. However, when I run any SP, I get initialization error:

System.TypeInitializationException: The type initializer for 'Lephone.Data.DbEntry' threw an exception.
---> System.TypeInitializationException: The type initializer for 'Lephone.Data.Common.EntryConfig' threw an exception.
---> Lephone.Util.SettingException: DataBase must be set as a valid value. Current get is : DataBase
at Lephone.Data.Common.EntryConfig.GetDriver(String Prefix)
at Lephone.Data.Common.EntryConfig..cctor()
--- End of inner exception stack trace ---
at Lephone.Data.DbEntry..cctor()
--- End of inner exception stack trace ---
at Lephone.Data.DbEntry.get_Context()
at Lephone.Data.Definition.DbObjectModelBase`2.FindBySql(String SqlStr)
at StoredProcedures.FindTag(String sName, String mc)

I am not sure what I am missing here. How can I use embeded xml file to provide configuration, specially connection string?
Coordinator
Feb 18, 2008 at 12:02 AM
The file name of this config file must have a ".config.xml" postfix.
For example:
myconn.config.xml
Feb 18, 2008 at 1:30 AM
Thank you for your reply. I did change my config file to app.config.xml as embeded resource. Here is a different error message:

System.TypeInitializationException: The type initializer for 'Lephone.Data.DbEntry' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Lephone.Data.Common.EntryConfig' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Lephone.Data.Common.DataSetting' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Lephone.Util.Setting.ConfigHelper' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Unrecognized element.
at System.Configuration.HandlerBase.ThrowUnrecognizedElement(XmlNode node)
at System.Configuration.NameValueSectionHandler.CreateStatic(Object parent, XmlNode section, String keyAttriuteName, String valueAttributeName)
at System.Configuration.NameValueSectionHandler.Create(Object parent, Object context, XmlNode section)
at Lephone.Util.Setting.ResourceConfigReader.ParseConfig(String s)
at Lephone.Util.Setting.ResourceConfigReader.InitAllXmlConfigFiles()
at Lephone.Util.Setting.ResourceConfigReader.GetSection(String SectionName)
at Lephone.Util.Setting.ConfigReaderProxy.GetSection(String SectionName)
at Lephone.Util.Setting.ConfigHelper..ctor(String SectionName)
at Lephone.Util.Setting.ConfigHelper..cctor()
--- End of inner exception stack trace ---
at Lephone.Data.Common.DataSetting..cctor()
--- End of inner exception stack trace ---
at Lephone.Data.Common.EntryConfig..cctor()
--- End of inner exception stack trace ---
at Lephone.Data.DbEntry..cctor()
--- End of inner exception stack trace ---
at Lephone.Data.DbEntry.get_Context()
at Lephone.Data.Definition.DbObjectModelBase`2.FindBySql(String SqlStr)
at StoredProcedures.FindTag1(String sName, String mc)

I removed the first line of <?xml version="1.0" encoding="utf-8" ?> in config file, still I got the exception. Somewhere wrong in xml file?
Feb 18, 2008 at 2:31 AM
I think I find the problem which caused the exception: removing the section of <configurations>. In the embeded xml configuration file, only one section of <Lephone.Settings> should be left within <configuration>.

Even with the this change, I still encountered a different exception:

System.TypeInitializationException: The type initializer for 'Lephone.Data.DbEntry' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Lephone.Data.Common.EntryConfig' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Lephone.Data' or one of its dependencies. The system cannot find the file specified.
File name: 'Lephone.Data'
at System.RuntimeTypeHandle._GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean loadTypeFromPartialName)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
at System.RuntimeType.PrivateGetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
at System.Type.GetType(String typeName, Boolean throwOnError)
at Lephone.Util.ClassHelper.CreateInstance(String ClassName)
at Lephone.Data.Common.EntryConfig.GetDriver(String Prefix)
at Lephone.Data.Common.EntryConfig..cctor()

I am not sure if this is the limitation of SQL server project or not. It might be caused by missing dependencies. I realize that Lephone.Util has a reference to System.Configuration.dll. I tried to registrer this dll to SQL Server assemblies but it failed. SQL server complains this dll is system assembly, and recommended to use a wrapper assembly for the desired functionalities.

I tried NHibernate and had terrible failures as well. The end result looks similar. As I memtioned, it might be an issue fo SQL Server project limitation. Any suggestions?
Coordinator
Feb 18, 2008 at 2:46 AM
Yes, remove configurations section is right way to handle it.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <Lephone.Settings>
        <add key="DataBase" value="@SqlServer2005 : Context Connection=true" />
    </Lephone.Settings>
</configuration>

I didn't tested the SQL Server project before.
For the new exception of it.
Please try to deploy "Lephone.Data.dll" and "Lephone.Util.dll" to GAC.
Maybe it works.
Feb 18, 2008 at 4:07 AM
I just tried a simple console application with embeded xml file, and I can pass the exception point. Therefore, I think it is the limitation of SQL Server Project. It fails to make assembly reflection calls to get object from a type. That's too sad. There may be a way to do it.

By the way, I am new to this DbEntry. If I define a class mapping to a table in SQL, should I define all the properties to the columns in the table? Can I only define partial ones I am interested in? For example, a User table has id, fistName, LastName, and birthDate. Can I define a User class with only Id and FirstName in the class mapping to id and firstName in the SQL table?

The reason I am asking this question is that I got an exception (something related to index) when I tried to use DbEntry to make a query to get a list of Users back. I am not sure if I should define a User class in the same structure as the User table in SQL?
Coordinator
Feb 18, 2008 at 6:15 AM
Yes, in DbEntry, we can map partial columns in our class.
But if we use this class to insert an item to table, it may cause an exception from database.
For example, a User class with only Id and FirstName:

public abstract class User : DbObjectModel
{
    public abstract string FirstName { get; set; }
}