+1  A: 

That is odd. Maybe you could special case your web part to not do the database call when the page is in edit/design mode. Wouldn't that fix your problem?

Kirk Liemohn
I suppose I could do that. As I mentioned, I moved the code into OnInit and that "fixed" it. I just want to know why does it run a constructor with a minimal set of permissions when I granted all.
Ruslan
+2  A: 

Try setting up a solution package to deploy code access security settings. You definitely need SqlClientPermission. Check out my post on Code Access Security for web parts for more information.

Corey Roth
Thank you. However, I have. I tried with it and without. And issue here is not that CAS is misconfigured, since if it was nothing would work at all. It's that SharePoint disregards any configuration and runs w/min trust when first time adding a web part.
Ruslan
+1  A: 

As a simple test, you could deploy the assemby containing the web part into the GAL. There it will run with Full Trust.

Personally, I would not recommend establishing a database connection in the constructor of a web part unless you had a compelling reason to do so.

[updated 3009-03-27]

Debugging is often about ruling out causes. Even though you believe the web part is running in Full Trust, I recommend testing the GAC'ed case. Remember to remove the .dll from the Bin directory for the test. This will only take 5-10 minutes and you'll know where you stand.

Jason Weber
I haven't tried GAL yet, since the web part runs in FullTrust, just not when added the first time. I will try. I access DB in the constructor because certain design-time properties are dependent on user specific data. However, any privileged method fails, not just DB.
Ruslan