|
hi am new to linq i want to be able to insert manny rows transactiondetail with this code
ctx.MasterTransaction.InsertOnSubmit(tranmaster)
ctx.Submitchange()
For Each td As TransactionDetail In transdetail
td.TransId = tranmaster.TransId
Next
ctx.TransactionDetails.InsertAllOnSubmit (transdetail)
ctx.SubmitChanges()
i get this error 'Cannot add an entity with a key that is already in use' on submit changes
|
|
|
|
|
hi am new to ling i want to be able to insert manny rows transactiondetail with this code
ctx.MasterTransaction.InsertOnSubmit(tranmaster)
ctx.Submitchange()
For Each td As TransactionDetail In transdetail
td.TransId = tranmaster.TransId
Next
ctx.TransactionDetails.InsertAllOnSubmit (transdetail)
ctx.SubmitChanges()
i get this error 'Cannot add an entity with a key that is already in use' on submit changes
|
|
|
|
|
Hi,
I just wanted some clarification for using statement. With reference to my below code, lets say for some reason there is any exception on the following line of code - command.Parameters.Add("@ID", SqlDbType.Int)
So in case of exception, the lines of code after the above line of code will not get executed and the control will move on to the last line of the method. Considering this scenario, I wanted to know will the connection and command objects (which are already initialised) get disposed?
using (DbConnection connection =
this.Database.ConnectionManager.GetConnection())
{
using (DbCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "spGetList";
command.Parameters.Add("@ID", SqlDbType.Int)
command.Parameters("@ID").Value = this.ID
using(DbDataReader dataReader =
this.Database.Execute<DbDataReader>(command,
DatabaseExecutionType.DataReader))
{
this.Fill(dataReader);
}
}
}
Regards,
Vipul Mehta
|
|
|
|
|
The Using statement usually cleans up[^], that is, if your object is disposable.
Alas, the documentation [^]for SqlConnection states;
MSDN wrote: If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose. Close and Dispose are functionally equivalent. If the connection pooling value Pooling is set to true or yes, the underlying connection is returned back to the connection pool. On the other hand, if Pooling is set to false or no, the underlying connection to the server is actually closed.
I are Troll
|
|
|
|
|
using will ensure you to dispose an IDisposable-derived object even if you return from inside the "using" block.
Best regards,
Jaime.
|
|
|
|
|
connection , command and dataReader will all have their Dispose or IDisposable.Dispose methods called.
|
|
|
|
|
In general, 'Using' is an almost magic bullet to ensure that allocated resources will get freed when used as indicated above. It's not always perfect, however. The biggest gotcha I have found is that a constructor itself creates iDisposable objects which are supposed to get disposed at a later time and an exception is thrown in the constructor, the constructor itself must dispose of those objects, since the partially-constructed object will be going out of scope.
For example, using VB syntax:
Sub New(whatever_arguments...)
Try
.. construct the object
Catch
Me.Dispose()
Throw
End Try
End Sub
Note that the catch block doesn't swallow an exception nor even throw a new one; it simply re-throws the old exception so as to preserve the stack trace.
|
|
|
|
|
We can use .net class (Manage code) in using clause it will automatically goes out of scope. You don't need to close or dispose out side, Because they called dispose function.all the class which is implemented the idisposable intercace will work on using clause
|
|
|
|
|
using keyword is nothing but a syntactic sugar over try-finally construct. For example, the following code
using (SqlConnection conn = new SqlConnection(...)) {
....
....
....
}
actually gets compiled to
SqlConnection conn = new SqlConnection(...);
try {
....
....
....
}
finally {
if (conn is IDisposable) {
conn.Dispose();
}
}
Therefore, irrespective of whether an exception is thrown or a return statement is encountered, the finally blok is always executed and ensures that objects that implement IDisposable are always cleaned up (unless the runtime host chooses not to do so - just to complicate things).
|
|
|
|
|
Exception message:
"Unrecognized attribute 'valid'. Note that attribute names are case-sensitive.
I used to have a field called "valid" in one of my configuration elements, but through the course of development it's now obsolete, so I'd like to remove it. I removed it from the ConfigurationElement subclass, and now when I try to read the config section (via config.GetSection), I get the above exception, which then makes my config section variable null, blah blah blah, no saved config = program effectively unusable from session to session.
Google fails me. MSDN fails me. Internet fails me.
I'm new to the ConfigurationManager, having only ever done equivalent functionality through .ini files before, so I'm sure there's probably just some "cleanup" command I can do that I'm just not aware of.
modified on Friday, October 23, 2009 2:41 PM
|
|
|
|
|
I believe you are writing a class to read configuration values from a config class. You have removed the 'valid' element from the spec but not from the config file. I think, removing the 'valid' element from the config file should solve your problem.
|
|
|
|
|
Yes, that's more or less the problem, but since it's using the ConfigurationManager, the actual config file operations are completely hidden (as far as I know), or at least I don't know where to find it.
|
|
|
|
|
What kind of app r u developing? Windows or Web ? Whatever it is you should have full control over your config files.
|
|
|
|
|
It's a Windows app using the built-in ConfigurationManager .NET capabilities. I based it on this MSDN code sample[^]. Maybe it'll be easier to just show some code:
Configuration classes:
public class SerialConfig : ConfigurationElement
{
[ConfigurationProperty("valid",
DefaultValue = false,
IsRequired = false)]
public bool Valid
{
get
{
return (bool)this["valid"];
}
set
{
this["valid"] = (bool)value;
}
}
[ConfigurationProperty("baud",
DefaultValue = 115200,
IsRequired = true)]
public int Baud
{
get
{
return (int)this["baud"];
}
set
{
if((value == 115200) || (value == 9600))
{
this["baud"] = value;
}
else
{
}
}
}
[ConfigurationProperty("name",
DefaultValue = "",
IsRequired = true)]
public String Name
{
get
{
return (string)this["name"];
}
set
{
if (value.Contains("COM"))
{
this["name"] = value;
}
else
{
this["name"] = "INVALID";
}
}
}
public SerialConfig(String name, int baud)
{
this["baud"] = baud;
this["name"] = name;
}
public SerialConfig(string name)
{
new SerialConfig(name, 115200);
}
public SerialConfig()
{
new SerialConfig("INVALID", 115200);
}
}
public class SerialConfigSection : ConfigurationSection
{
public SerialConfigSection()
{
this["serialConfig"] = new SerialConfig();
}
[ConfigurationProperty("serialConfig")]
public SerialConfig serialConfig
{
get
{
return (
(SerialConfig)this["serialConfig"]);
}
set
{
this["serialConfig"] = value;
}
}
}
Actual usage of configuration
public static void InitializeConfig()
{
Configuration userConfig =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal);
ExeConfigurationFileMap configFileMap =
new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename =
userConfig.FilePath;
config =
ConfigurationManager.OpenMappedExeConfiguration(
configFileMap, ConfigurationUserLevel.None);
EITPortConfig = null;
BridgePortConfig = null;
try
{
EITPortConfig = (SerialConfigSection)config.GetSection("EITCOMPort");
BridgePortConfig = (SerialConfigSection)config.GetSection("BridgeCOMPort");
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("EITCOMPort");
ConfigurationManager.RefreshSection("BridgeCOMPort");
if (EITPortConfig == null)
{
EITPortConfig = new SerialConfigSection();
EITPortConfig.SectionInformation.AllowExeDefinition =
ConfigurationAllowExeDefinition.MachineToLocalUser;
EITPortConfig.SectionInformation.AllowOverride = true;
config.Sections.Add("EITCOMPort", EITPortConfig);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("EITCOMPort");
}
if (BridgePortConfig == null)
{
BridgePortConfig = new SerialConfigSection();
BridgePortConfig.SectionInformation.AllowExeDefinition =
ConfigurationAllowExeDefinition.MachineToLocalUser;
BridgePortConfig.SectionInformation.AllowOverride = true;
config.Sections.Add("BridgeCOMPort", BridgePortConfig);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("BridgeCOMPort");
}
}
catch (ConfigurationErrorsException e)
{
Console.WriteLine("[Exception error: {0}]", e.ToString());
}
}
|
|
|
|
|
Your solution must have a default configuration file (app.config) that is used as a template to create local copies during deployment. For example, if you app name is myapp.exe, then your configuration file name during deployment is myapp.exe.config. Changing the app.config should solve your problem.
|
|
|
|
|
In the InitializeConfig() method, debug and check the value of userConfig.FilePath property. I think that should tell you the configuration file path.
|
|
|
|
|
Thank you! That finally resolved the issue. I knew it had to be lingering around somewhere.
|
|
|
|
|
Hi all,
I think this can trigger a nice discussion about .net capabilities.
I want to have a big single assembly with dozens of functionality groups, but I don't want them to be uploaded into memory until they are really needed.
This is a proof of concept, trying to demonstrate the best way to deploy a multi-mission application that has all possible functionalities in a single assembly, and, according to configuration, will run use specific classes.
The purpose is to ensure to distribute all functionalities in a single file and avoid branching problems without memory costs.
Thanks in advance.
Best regards,
Jaime.
|
|
|
|
|
Just put all your assemblies in a zipfile and load them on demand. You can also put them in a SqlCE database and load them from a BLOB-field
"The city’s central computer told you? R2D2, you know better than to trust a strange computer!"
-- C3PO
modified on Tuesday, September 8, 2009 3:20 AM
|
|
|
|
|
Hmm, not sure if it will be practical. Database approach is discarded, but embedding dlls into a single assembly can have some benefits. Still have to think on that, specially how to avoid to add complexity to the "consumer" code.
Best regards,
Jaime.
|
|
|
|
|
Jaime Olivares wrote: Hmm, not sure if it will be practical.
I found that multiple assemblies are more practical; you load the pieces you need, and keep 'em together in a folder. One could also compile everything into a massive DLL, but that's harder to update (you'd have to replace the entire thing, or overwrite a piece of it with binary data) and I doubt that .NET will optimize the memory-usage.
Jaime Olivares wrote: embedding dlls into a single assembly can have some benefits.
For distribution, yes
I fail to see the advantages for a deployed application yet. Perhaps you can explain that a bit more in detail?
"If you automate a mess, you get an automated mess."
-- Rod Michael
|
|
|
|
|
Eddy Vluggen wrote: One could also compile everything into a massive DLL, but that's harder to update (you'd have to replace the entire thing
That's the idea! I don't want to replace one or two dlls and leave others untouched. At some point, this becomes a mess which is hard to manage.
Best regards,
Jaime.
|
|
|
|
|
Jaime Olivares wrote: At some point, this becomes a mess which is hard to manage.
How so?
Jaime Olivares wrote: I don't want to replace one or two dlls and leave others untouched
If they haven't changed, why update them?
only two letters away from being an asset
|
|
|
|
|
I have dozens of products, all them sharing dozens of components. I can update a component that affect many products, of just specialized functionality that affects one specific product.
My intention is to have a secure distribution of the components' suite, prefereably as a monolithic big component, besides the specialized functionality that with a plugin approach.
Best regards,
Jaime.
|
|
|
|
|
Jaime Olivares wrote: prefereably as a monolithic big component
Hello, the 80's are gone. This is the 21st century, time to update your skills for the real world.
only two letters away from being an asset
|
|
|
|